@kodai3/use-set-state
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

useSetState

React state hook that creates setState method which works similar to how this.setState works in class components—it merges object changes into current state.

Usage

import { useSetState } from "@kodai3/use-set-state";

const Demo = () => {
  const [state, setState] = useSetState({});

  return (
    <div>
      <pre>{JSON.stringify(state, null, 2)}</pre>
      <button onClick={() => setState({ hello: "world" })}>hello</button>
      <button onClick={() => setState({ foo: "bar" })}>foo</button>
      <button
        onClick={() => {
          setState((prevState) => ({
            count: (prevState.count || 0) + 1,
          }));
        }}
      >
        count
      </button>
    </div>
  );
};

Reference

const [state, setState] = useSetState({ cnt: 0 });

setState({ cnt: state.cnt + 1 });
setState((prevState) => ({
  cnt: prevState + 1,
}));

Readme

Keywords

Package Sidebar

Install

npm i @kodai3/use-set-state

Weekly Downloads

2

Version

0.0.3

License

MIT

Unpacked Size

5.15 kB

Total Files

8

Last publish

Collaborators

  • kodai