Skip to content

Commit 955526c

Browse files
committed
2.0.2. lazy callback
1 parent a9b470c commit 955526c

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
```
1416
import 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 [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`
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/)

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-state-with-callback",
3-
"version": "1.0.18",
3+
"version": "2.0.2",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useLayoutEffect } from 'react';
1+
import { useState, useEffect, useLayoutEffect, useRef } from 'react';
22

33
const useStateWithCallback = (initialState, callback) => {
44
const [state, setState] = useState(initialState);
@@ -16,6 +16,28 @@ const useStateWithCallbackInstant = (initialState, callback) => {
1616
return [state, setState];
1717
};
1818

19-
export { useStateWithCallbackInstant };
19+
const useStateWithCallbackLazy = initialValue => {
20+
const callbackRef = useRef(null);
21+
22+
const [value, setValue] = useState(initialValue);
23+
24+
useEffect(() => {
25+
if (callbackRef.current) {
26+
callbackRef.current(value);
27+
28+
callbackRef.current = null;
29+
}
30+
}, [value]);
31+
32+
const setValueWithCallback = (newValue, callback) => {
33+
callbackRef.current = callback;
34+
35+
return setValue(newValue);
36+
};
37+
38+
return [value, setValueWithCallback];
39+
};
40+
41+
export { useStateWithCallbackInstant, useStateWithCallbackLazy };
2042

2143
export default useStateWithCallback;

0 commit comments

Comments
 (0)