Skip to content

Commit 1f09d23

Browse files
committed
fix(update): run callback only on update, not on mount
1 parent 8279500 commit 1f09d23

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# useStateWithCallback React Hook
22

3-
[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/use-state-with-callback.svg)](https://greenkeeper.io/) ![NPM](https://img.shields.io/npm/l/use-state-with-callback.svg)
3+
[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/use-state-with-callback.svg)](https://greenkeeper.io/) ![NPM](https://img.shields.io/npm/l/use-state-with-callback.svg)
44

55
Custom hook to include a callback function for useState which was previously available for setState in class components. [Read more about it here.](https://www.robinwieruch.de/react-usestate-callback/)
66

@@ -13,24 +13,32 @@ Custom hook to include a callback function for useState which was previously ava
1313
**useStateWithCallback:**
1414

1515
```jsx
16-
import React from 'react';
16+
import * as React from 'react';
1717

1818
import useStateWithCallback from 'use-state-with-callback';
1919

2020
// Note: cannot be used on the server-side (e.g. Next.js)
2121
// import { useStateWithCallbackInstant } from 'use-state-with-callback';
2222

2323
const App = () => {
24-
const [count, setCount] = useStateWithCallback(0, count => {
25-
if (count > 1) {
26-
console.log('render, then callback.');
27-
console.log('otherwise use useStateWithCallbackInstant()');
24+
const [count, setCount] = useStateWithCallback(0, currentCount => {
25+
console.log('render, then callback.');
26+
console.log('otherwise use useStateWithCallbackInstant()');
27+
28+
if (currentCount > 1) {
29+
console.log('Threshold of over 1 reached.');
30+
} else {
31+
console.log('No threshold reached.');
2832
}
2933
});
3034

31-
// const [count, setCount] = useStateWithCallbackInstant(0, count => {
32-
// if (count > 1) {
33-
// console.log('render with instant callback.');
35+
// const [count, setCount] = useStateWithCallbackInstant(0, currentCount => {
36+
// console.log('render with instant callback.');
37+
38+
// if (currentCount > 1) {
39+
// console.log('Threshold of over 1 reached.');
40+
// } else {
41+
// console.log('No threshold reached.');
3442
// }
3543
// });
3644

@@ -48,12 +56,14 @@ const App = () => {
4856
</div>
4957
);
5058
};
59+
60+
export default App;
5161
```
5262

5363
**useStateWithCallbackLazy:**
5464

5565
```jsx
56-
import React from 'react';
66+
import * as React from 'react';
5767
import { useStateWithCallbackLazy } from 'use-state-with-callback';
5868

5969
const App = () => {

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": "3.0.0",
3+
"version": "3.0.1",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,31 @@ import * as React from 'react';
33
const useStateWithCallback = (initialState, callback) => {
44
const [state, setState] = React.useState(initialState);
55

6-
React.useEffect(() => callback(state), [state, callback]);
6+
const didMount = React.useRef(false);
7+
8+
React.useEffect(() => {
9+
if (didMount.current) {
10+
callback(state);
11+
} else {
12+
didMount.current = true;
13+
}
14+
}, [state, callback]);
715

816
return [state, setState];
917
};
1018

1119
const useStateWithCallbackInstant = (initialState, callback) => {
1220
const [state, setState] = React.useState(initialState);
1321

14-
React.useLayoutEffect(() => callback(state), [state, callback]);
22+
const didMount = React.useRef(false);
23+
24+
React.useLayoutEffect(() => {
25+
if (didMount.current) {
26+
callback(state);
27+
} else {
28+
didMount.current = true;
29+
}
30+
}, [state, callback]);
1531

1632
return [state, setState];
1733
};

0 commit comments

Comments
 (0)