Skip to content

Commit 250e202

Browse files
authored
Update README.md
1 parent aaa387c commit 250e202

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ Custom hook to combine all useReducer hooks for one global state container with
66

77
* [Example Application](https://github.com/the-road-to-learn-react/react-with-redux-philosophy)
88
* ["How to implement it"-tutorial](https://www.robinwieruch.de/redux-with-react-hooks/).
9-
* Philosophy [[1]](https://www.robinwieruch.de/react-state-usereducer-usestate-usecontext/)[[2]](https://www.robinwieruch.de/redux-with-react-hooks/)
9+
* Requirements: [reducer](https://www.robinwieruch.de/javascript-reducer/) and [useReducer](https://www.robinwieruch.de/react-usereducer-hook/) explained.
1010

1111
## Installation
1212

1313
`npm install use-combined-reducers`
1414

1515
## Usage
1616

17+
Create a global dispatch function and state object by initializing multiple `useReducer` hooks in `useCombinedReducers`:
18+
1719
```
20+
import React from 'react';
1821
import useCombinedReducers from 'use-combined-reducers';
1922
2023
const App = () => {
@@ -27,6 +30,64 @@ const App = () => {
2730
2831
...
2932
}
33+
34+
export default App;
35+
```
36+
37+
You can pass state and dispatch function down via [props](https://www.robinwieruch.de/react-pass-props-to-component/) or [React's Context API](https://www.robinwieruch.de/react-context-api/). Since passing it down with props is straight forward, the approach with context is demonstrated here. In some file:
38+
39+
```
40+
import React from 'react';
41+
42+
export const StateContext = React.createContext();
43+
export const DispatchContext = React.createContext();
44+
```
45+
46+
In your top-level React component (or any other component above a component tree which needs managed state):
47+
48+
```
49+
import React from 'react';
50+
import useCombinedReducers from 'use-combined-reducers';
51+
52+
import { StateContext, DispatchContext } from './somefile.js';
53+
54+
const App = () => {
55+
const [state, dispatch] = useCombinedReducers({
56+
myTodos: React.useReducer(todoReducer, initialTodos),
57+
myOtherStuff: React.useReducer(stuffReducer, initialStuff),
58+
});
59+
60+
return (
61+
<DispatchContext.Provider value={dispatch}>
62+
<StateContext.Provider value={state}>
63+
<SomeComponent />
64+
</StateContext.Provider>
65+
</DispatchContext.Provider>
66+
);
67+
}
68+
69+
export default App;
70+
```
71+
72+
In some other component which sits below the state/dispatch providing component:
73+
74+
```
75+
import React from 'react';
76+
77+
import { StateContext, DispatchContext } from './somefile.js';
78+
79+
export default () => {
80+
const state = React.useContext(StateContext);
81+
const dispatch = React.useContext(DispatchContext);
82+
83+
const { myTodos, myOtherStuff } = state;
84+
85+
return (
86+
<div>
87+
...
88+
</div>
89+
);
90+
};
3091
```
3192

3293
## Contribute

0 commit comments

Comments
 (0)