@@ -10,6 +10,8 @@ Custom hook to include a callback function for useState which was previously ava
1010
1111## Usage
1212
13+ ** useStateWithCallback:**
14+
1315```
1416import React from 'react';
1517
@@ -31,27 +33,64 @@ const App = () => {
3133 // }
3234 // });
3335
36+ const handleClick = () => {
37+ setCount(count + 1);
38+ };
39+
3440 return (
3541 <div>
3642 {count}
3743
38- <button type="button" onClick={() => setCount(count + 1) }>
44+ <button type="button" onClick={handleClick }>
3945 Increase
4046 </button>
4147 </div>
4248 );
4349};
4450```
4551
52+ ** useStateWithCallbackLazy:**
53+
54+ ```
55+ import React from 'react';
56+ import { useStateWithCallbackLazy } from 'use-state-with-callback';
57+
58+ const App = () => {
59+ const [count, setCount] = useStateWithCallbackLazy(0);
60+
61+ const handleClick = () => {
62+ setCount(count + 1, (currentCount) => {
63+ if (currentCount > 1) {
64+ console.log('Threshold of over 1 reached.');
65+ } else {
66+ console.log('No threshold reached.');
67+ }
68+ });
69+ };
70+
71+ return (
72+ <div>
73+ <p>{count}</p>
74+
75+ <button type="button" onClick={handleClick}>
76+ Increase
77+ </button>
78+ </div>
79+ );
80+ };
81+
82+ export default App;
83+ ```
84+
4685## Contribute
4786
48- * ` git clone git@github.com:the-road-to-learn-react/use-state-with-callback.git `
49- * ` cd use-state-with-callback `
50- * ` npm install `
51- * ` npm run test `
87+ - ` git clone git@github.com:the-road-to-learn-react/use-state-with-callback.git `
88+ - ` cd use-state-with-callback `
89+ - ` npm install `
90+ - ` npm run test `
5291
5392### More
5493
55- * [ Publishing a Node Package to NPM] ( https://www.robinwieruch.de/publish-npm-package-node/ )
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/ )
94+ - [ Publishing a Node Package to NPM] ( https://www.robinwieruch.de/publish-npm-package-node/ )
95+ - [ Node.js Testing Setup] ( https://www.robinwieruch.de/node-js-testing-mocha-chai/ )
96+ - [ React Testing Setup] ( https://www.robinwieruch.de/react-testing-tutorial/ )
0 commit comments