Skip to content
Custom hook to include a callback function for useState.
Branch: master
Clone or download
Latest commit af75ea8 Jun 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Create FUNDING.yml Jun 17, 2019
.nyc_output more May 31, 2019
src more May 31, 2019
test first commit May 31, 2019
.babelrc finish May 31, 2019
.gitignore first commit May 31, 2019
.npmignore more May 31, 2019
.prettierrc first commit May 31, 2019
.travis.yml first commit May 31, 2019
LICENSE first commit May 31, 2019
README.md Update README.md Jun 5, 2019
package-lock.json more May 31, 2019
package.json finish May 31, 2019
webpack.config.js Update webpack.config.js Jun 4, 2019

README.md

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 git@github.com:the-road-to-learn-react/use-state-with-callback.git
  • cd use-state-with-callback
  • npm install
  • npm run test

More

You can’t perform that action at this time.