Skip to content

Commit 0d8822f

Browse files
authored
Update README.md
1 parent 5cf5ccd commit 0d8822f

File tree

1 file changed

+46
-82
lines changed

1 file changed

+46
-82
lines changed

README.md

Lines changed: 46 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,76 @@
44

55
Very simple shared store for your react app.
66

7-
## Usage
7+
## 0.1.x => 0.2.x migration guide
88

9-
### Basic Usage ([WebpackBin](https://www.webpackbin.com/bins/-Kv6suDDJxqwug4rLpFh))
10-
1. Add provider to keep our state
11-
2. connect function to use with this provider
9+
**Important!** 0.2 has changed `connect`'s behavior.
1210

13-
```javascript
14-
import React from 'react'
15-
import { getProvider, connect } from 'react-shared-state'
16-
17-
const PROVIDER_KEY = 'simple_provider'
18-
const SimpleProvider = getProvider(PROVIDER_KEY)
19-
20-
21-
const mapStateToProps = (state, props) => ({ name: state.name })
22-
@connect(PROVIDER_KEY, mapStateToProps)
23-
class Hello extends React.Component {
24-
render = () => (
25-
<div>
26-
<h1> Hello, {this.props.name} </h1>
27-
<button onClick={() => this.props[PROVIDER_KEY].setState({ name: 'Max' })}>Set Name</button>
28-
</div>
29-
)
30-
}
11+
1. mapStateToProps now accepts `store` instead of `state.store` as first argument
12+
2. `connect` no longer passes `store` by it's name to a component
3113

32-
export function App() {
33-
return (
34-
<SimpleProvider initialState={{ name: 'Anonymous' }}>
35-
<Hello />
36-
</SimpleProvider>
37-
)
38-
}
39-
```
4014

41-
### Extending State ([WebpackBin](https://www.webpackbin.com/bins/-Kv6uj9SWKoHoV8Oz9P2))
15+
## Usage
16+
17+
### Extending State ([WebpackBin](https://codesandbox.io/s/qqk6lq7xj9))
4218

4319
```javascript
44-
import React, { Component } from 'react'
45-
import { getProvider, connect, SharedStore } from 'react-shared-state'
20+
import React, { Component } from "react";
21+
import ReactDOM from "react-dom";
22+
import { getProvider, SharedStore } from "react-shared-state";
4623

4724
class GithubIssuesStore extends SharedStore {
25+
state = {
26+
loading: false,
27+
issuesCount: 0
28+
};
29+
4830
loadCountFromGithub = () => {
49-
this.setState({ loading: true })
31+
this.setState({ loading: true });
5032

51-
fetch('https://api.github.com/repos/vmg/redcarpet/issues?state=closed')
52-
.then((resp) => resp.json())
53-
.then((data) => {
33+
fetch("https://api.github.com/repos/vmg/redcarpet/issues?state=closed")
34+
.then(resp => resp.json())
35+
.then(data => {
5436
this.setState({
5537
issuesCount: data.length,
5638
loading: false
57-
})
58-
})
59-
}
39+
});
40+
});
41+
};
6042

61-
resetCount = () => this.setState({ issuesCount: 0 })
62-
incrementCount = () => this.setState({ issuesCount: this.state.issuesCount += 1 })
43+
resetCount = () => this.setState({ issuesCount: 0 });
44+
incrementCount = () => this.setState({ issuesCount: this.state.issuesCount += 1 });
6345
}
6446

47+
const GithubProvider = getProvider("github", GithubIssuesStore);
6548

66-
const PROVIDER_KEY = 'github'
67-
const GithubProvider = getProvider(PROVIDER_KEY, GithubIssuesStore)
68-
69-
@connect(PROVIDER_KEY, (state, props) => ({ issuesCount: state.issuesCount, loading: state.loading }))
49+
@GithubProvider.connect((store, props) => ({
50+
issuesCount: store.state.issuesCount,
51+
loading: store.state.loading
52+
}))
7053
class IssuesCount extends React.Component {
7154
render = () => {
72-
const { issuesCount, loading } = this.props
73-
return (
74-
<h1>
75-
Issues count: {loading ? '...' : issuesCount}
76-
</h1>
77-
)
78-
}
55+
const { issuesCount, loading } = this.props;
56+
return <h1>Issues count: {loading ? "..." : issuesCount}</h1>;
57+
};
7958
}
8059

81-
8260
// We can use compose as a HOC call
83-
const ControlButtonsComponent = (props) => (
61+
const ControlButtonsComponent = props => (
8462
<div>
85-
<button onClick={() => props.github.loadCountFromGithub()}>
63+
<button onClick={() => props.loadCountFromGithub()}>
8664
Load From GitHub
8765
</button>
88-
<button onClick={() => props.github.resetCount()}>
89-
Reset
90-
</button>
91-
<button onClick={() => props.github.incrementCount()}>
92-
Increment
93-
</button>
66+
<button onClick={() => props.resetCount()}>Reset</button>
67+
<button onClick={() => props.incrementCount()}>Increment</button>
9468
</div>
95-
)
96-
const ControlButtons = connect(PROVIDER_KEY)(ControlButtonsComponent)
97-
98-
99-
export default class App extends Component {
69+
);
70+
const ControlButtons = GithubProvider.connect(store => ({
71+
loadCountFromGithub: store.loadCountFromGithub,
72+
resetCount: store.resetCount,
73+
incrementCount: store.incrementCount
74+
}))(ControlButtonsComponent);
75+
76+
export class App extends Component {
10077
render() {
10178
return (
10279
<div>
@@ -105,24 +82,11 @@ export default class App extends Component {
10582
<ControlButtons />
10683
</GithubProvider>
10784
</div>
108-
)
85+
);
10986
}
11087
}
111-
```
88+
ReactDOM.render(<App />, document.getElementById("root"));
11289

113-
### Using custom connect
114-
It's very useful to create custom `connect` function to connect to specified provider
115-
```javascript
116-
const PROVIDER_KEY = 'simple_provider'
117-
const SimpleProvider = getProvider(PROVIDER_KEY)
118-
const simpleConnect = (...args) => connect(PROVIDER_KEY, ...args)
119-
120-
const mapStateToProps = (state, props) => ({ name: state.name })
121-
122-
@simpleConnect(mapStateToProps)
123-
class Hello extends React.Component {
124-
...
125-
}
12690
```
12791

12892

0 commit comments

Comments
 (0)