@barelyhuman/set-state-hook
v0.0.5
Published
Simple setState equivalent with mutable drafts
Downloads
25
Maintainers
Readme
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