@barelyhuman/set-state-hook

0.0.5 • Public • Published

set-state-hook

Simple setState equivalent with mutable drafts

If you like any of my work, you can support me on: https://barelyhuman.dev/donate

GitHub JavaScript Style Guide Build Size Version Downloads

Install

$ npm install @barelyhuman/set-state-hook
# or
$ yarn add @barelyhuman/set-state-hook

Usage

import { useSetState } from "@barelyhuman/set-state-hook";

function Component() {
  const [state, setState] = useSetState({
    name: "Reaper",
    age: 16,
  });

  React.useEffect(() => {
    setState({
      name: "Hello World",
    });
  }, [setState]);

  // or

  React.useEffect(() => {
    setState((prevState, draftState) => {
      draftState.name = "Hello World";
      return draftState;
    });
  }, [setState]);

  return (
    <>
      <p>name: {state.name}</p>
      <p>age: {state.age}</p>
    </>
  );
}

Features

  • Easier state management for dependent state fields
  • Handle complex state updates using functional state handlers while keeping the simplicity of mutable objects
  • Tree Shakable
  • Small, 1.1kb GZipped

License

MIT © Reaper

Package Sidebar

Install

npm i @barelyhuman/set-state-hook

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

13 kB

Total Files

5

Last publish

Collaborators

  • barelyreaper