@kingerez/usestatewithcallback
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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);
});

Readme

Keywords

Package Sidebar

Install

npm i @kingerez/usestatewithcallback

Weekly Downloads

1

Version

1.0.2

License

ISC

Unpacked Size

13.8 kB

Total Files

5

Last publish

Collaborators

  • kingerez