1
1
# useStateWithCallback React Hook
2
2
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 )
4
4
5
5
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/ )
6
6
@@ -13,24 +13,32 @@ Custom hook to include a callback function for useState which was previously ava
13
13
** useStateWithCallback:**
14
14
15
15
``` jsx
16
- import React from ' react' ;
16
+ import * as React from ' react' ;
17
17
18
18
import useStateWithCallback from ' use-state-with-callback' ;
19
19
20
20
// Note: cannot be used on the server-side (e.g. Next.js)
21
21
// import { useStateWithCallbackInstant } from 'use-state-with-callback';
22
22
23
23
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.' );
28
32
}
29
33
});
30
34
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.');
34
42
// }
35
43
// });
36
44
@@ -48,12 +56,14 @@ const App = () => {
48
56
< / div>
49
57
);
50
58
};
59
+
60
+ export default App ;
51
61
```
52
62
53
63
** useStateWithCallbackLazy:**
54
64
55
65
``` jsx
56
- import React from ' react' ;
66
+ import * as React from ' react' ;
57
67
import { useStateWithCallbackLazy } from ' use-state-with-callback' ;
58
68
59
69
const App = () => {
0 commit comments