@kingerez/usestatewithcallback
v1.0.2
Published
Use this package to add a callback whenever setting the current state using the useState method
Downloads
6
Readme
useStateWithCallback
I created this package to enable developers to use a functionality similar to what we used to have with the setState method.
Important caviat
Because of the way React works, the callback being called exists on the previous instantiation of the component. So I've updated the callback function to also pass the updated state.
Installation
npm i @kingerez/usestatewithcallback
-or-
yarn add @kingerez/usestatewithcallback
Import
import { useStateWithCallback } from '@kingerez/usestatewithcallback';
Usage
const [counter, setCounter] = useStateWithCallback(0);
setCounter(5, (newValue) => {
console.log('Counter state updated. Current value:', newValue);
});