Skip to main content

useSetState

useState wrapper to work with state like in class component

Usage

Live Editor
function Demo() {
  const [state, setState] = useSetState({ value1: "value1", value2: "value2" });
  const { value1, value2 } = state;
  return (
    <div>
      <p>value1: {value1}</p>
      <p>value2: {value2}</p>
      <button
        onClick={() => {
          setState({
            value1: "value",
          });
        }}
      >
        change value
      </button>
    </div>
  );
};

Result
Loading...

API

useSetState

Returns

readonly [T, (statePartial: Partial<T> | ((currentState: T) => Partial<T>)) => void]: A tuple with the following elements:

  • The current value of the state.
  • A function to update the value of the state.

Arguments

ArgumentDescriptionTypeDefaultValue
initialStateinitial valueT (Required)-