useSetState
This hook is like this.setState
method of class component. It requires an object as the initial state, and can update part of the state. The new state will be merged with the old state.
API
function useSetState<S extends object>(
initialState: S | (() => S)
): [
state: S,
setState: (patch: Partial<S> | ((prevState: S) => Partial<S>)) => void
];
Example
0
0
import React from "react";
import { useSetState } from "@lilib/hooks";
function Example() {
const [state, setState] = useSetState({ count1: 0, count2: 0 });
const { count1, count2 } = state;
return (
<>
<div>
<button
onClick={() => {
setState({ count1: count1 + 1 });
}}
>
+1
</button>{" "}
{count1}
</div>
<div>
<button
onClick={() => {
setState({ count2: count2 + 2 });
}}
>
+2
</button>{" "}
{count2}
</div>
</>
);
}
export default Example;