|
1 | | -# useCombinedReducers React Hook |
| 1 | +# useStateWithCallback React Hook |
2 | 2 |
|
3 | | -[](https://travis-ci.org/the-road-to-learn-react/use-combined-reducers) [](https://slack-the-road-to-learn-react.wieruch.com/) [](https://greenkeeper.io/) [](https://coveralls.io/github/the-road-to-learn-react/use-combined-reducers?branch=master)  |
| 3 | +[](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [](https://slack-the-road-to-learn-react.wieruch.com/) [](https://greenkeeper.io/) [](https://coveralls.io/github/the-road-to-learn-react/use-state-with-callback?branch=master)  |
4 | 4 |
|
5 | | -Custom hook to combine all useReducer hooks for one global state container with one dispatch function. Use at top-level and pass dispatch function (and state) down via React's Context API with Provider and Consumer/useContext. |
6 | | - |
7 | | -* [Example Application](https://github.com/the-road-to-learn-react/react-with-redux-philosophy) |
8 | | -* ["How to implement it"-tutorial](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. |
| 5 | +Custom hook to include a callback function for useState which was previously available for setState in class components. |
10 | 6 |
|
11 | 7 | ## Installation |
12 | 8 |
|
13 | | -`npm install use-combined-reducers` |
| 9 | +`npm install use-state-with-callback` |
14 | 10 |
|
15 | 11 | ## Usage |
16 | 12 |
|
17 | | -Create a global dispatch function and state object by initializing multiple `useReducer` hooks in `useCombinedReducers`: |
18 | | - |
19 | 13 | ``` |
20 | 14 | import React from 'react'; |
21 | | -import useCombinedReducers from 'use-combined-reducers'; |
22 | | -
|
23 | | -const App = () => { |
24 | | - const [state, dispatch] = useCombinedReducers({ |
25 | | - myTodos: React.useReducer(todoReducer, initialTodos), |
26 | | - myOtherStuff: React.useReducer(stuffReducer, initialStuff), |
27 | | - }); |
28 | | -
|
29 | | - const { myTodos, myOtherStuff } = state; |
30 | 15 |
|
31 | | - ... |
32 | | -} |
33 | | -
|
34 | | -export default App; |
35 | | -``` |
| 16 | +import useStateWithCallback from 'use-state-with-callback'; |
36 | 17 |
|
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'; |
| 18 | +// import { useStateWithCallbackInstant } from 'use-state-with-callback'; |
53 | 19 |
|
54 | 20 | const App = () => { |
55 | | - const [state, dispatch] = useCombinedReducers({ |
56 | | - myTodos: React.useReducer(todoReducer, initialTodos), |
57 | | - myOtherStuff: React.useReducer(stuffReducer, initialStuff), |
| 21 | + const [count, setCount] = useStateWithCallback(0, count => { |
| 22 | + if (count > 1) { |
| 23 | + console.log('render, then callback.'); |
| 24 | + console.log('otherwise use useStateWithCallbackInstant()'); |
| 25 | + } |
58 | 26 | }); |
59 | 27 |
|
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; |
| 28 | + // const [count, setCount] = useStateWithCallbackInstant(0, count => { |
| 29 | + // if (count > 1) { |
| 30 | + // console.log('render with instant callback.'); |
| 31 | + // } |
| 32 | + // }); |
84 | 33 |
|
85 | 34 | return ( |
86 | 35 | <div> |
87 | | - ... |
| 36 | + {count} |
| 37 | +
|
| 38 | + <button type="button" onClick={() => setCount(count + 1)}> |
| 39 | + Increase |
| 40 | + </button> |
88 | 41 | </div> |
89 | 42 | ); |
90 | 43 | }; |
91 | 44 | ``` |
92 | 45 |
|
93 | 46 | ## Contribute |
94 | 47 |
|
95 | | -* `git clone git@github.com:the-road-to-learn-react/use-combined-reducers.git` |
96 | | -* `cd use-combined-reducers` |
| 48 | +* `git clone git@github.com:the-road-to-learn-react/use-state-with-callback.git` |
| 49 | +* `cd use-state-with-callback` |
97 | 50 | * `npm install` |
98 | 51 | * `npm run test` |
99 | 52 |
|
100 | 53 | ### More |
101 | 54 |
|
102 | 55 | * [Publishing a Node Package to NPM](https://www.robinwieruch.de/publish-npm-package-node/) |
103 | 56 | * [Node.js Testing Setup](https://www.robinwieruch.de/node-js-testing-mocha-chai/) |
| 57 | +* [React Testing Setup](https://www.robinwieruch.de/react-testing-tutorial/) |
0 commit comments