Skip to content

anicarrr/use-state-with-callback

 
 

Repository files navigation

useStateWithCallback React Hook

Build Status Slack Greenkeeper badge Coverage Status NPM

Custom hook to include a callback function for useState which was previously available for setState in class components. Read more about it here.

Installation

npm install use-state-with-callback

Usage

import React from 'react';

import useStateWithCallback from 'use-state-with-callback';

// import { useStateWithCallbackInstant } from 'use-state-with-callback';

const App = () => {
  const [count, setCount] = useStateWithCallback(0, count => {
    if (count > 1) {
      console.log('render, then callback.');
      console.log('otherwise use useStateWithCallbackInstant()');
    }
  });

  // const [count, setCount] = useStateWithCallbackInstant(0, count => {
  //   if (count > 1) {
  //     console.log('render with instant callback.');
  //   }
  // });

  return (
    <div>
      {count}

      <button type="button" onClick={() => setCount(count + 1)}>
        Increase
      </button>
    </div>
  );
};

Contribute

  • git clone [email protected]:the-road-to-learn-react/use-state-with-callback.git
  • cd use-state-with-callback
  • npm install
  • npm run test

More

About

Custom hook to include a callback function for useState.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%