@@ -10,6 +10,8 @@ Custom hook to include a callback function for useState which was previously ava
10
10
11
11
## Usage
12
12
13
+ ** useStateWithCallback:**
14
+
13
15
```
14
16
import React from 'react';
15
17
@@ -31,27 +33,64 @@ const App = () => {
31
33
// }
32
34
// });
33
35
36
+ const handleClick = () => {
37
+ setCount(count + 1);
38
+ };
39
+
34
40
return (
35
41
<div>
36
42
{count}
37
43
38
- <button type="button" onClick={() => setCount(count + 1) }>
44
+ <button type="button" onClick={handleClick }>
39
45
Increase
40
46
</button>
41
47
</div>
42
48
);
43
49
};
44
50
```
45
51
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
+
46
85
## Contribute
47
86
48
- * ` git clone [email protected] :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 [email protected] :the-road-to-learn-react/use-state-with-callback.git `
88
+ - ` cd use-state-with-callback `
89
+ - ` npm install `
90
+ - ` npm run test `
52
91
53
92
### More
54
93
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