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
55Custom 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
1818import 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
2323const 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' ;
5767import { useStateWithCallbackLazy } from ' use-state-with-callback' ;
5868
5969const App = () => {
0 commit comments