Neverending Programming Mistakes

    use-state-with-effect
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.0 • Public • Published

    use-state-with-effect

    Attach effect callbacks to useState updates.

    Motivation

    Sometimes you have some related code that should run in parallel with your state updates. While useEffect is the default way to achieve this, it does involve some ceremony, and may also hinder colocation.

    use-state-with-effect solves this by exposing a simple function, withEffect. This function merely takes the result returned from useState and a callback function, and then wraps wraps the update function with the provided effect.

    Usage

    Getting started is quick and simple!

    npm install use-state-with-effect

    Then, in your component:

    import { withState } from 'use-state-with-effect';
     
    const MyComponent = () => {
      const [state, setState] = withEffect(useState(), (prevValue, newValue) => {
        console.log({ prevValue, newValue });
      });
    };

    Keywords

    none

    Install

    npm i use-state-with-effect

    DownloadsWeekly Downloads

    1

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    8.95 kB

    Total Files

    13

    Last publish

    Collaborators

    • janove