tsuite
    Preparing search index...

    Function createState

    • Creates a stateful value with an associated side-effect that runs whenever the state is updated.

      Type Parameters

      • T

        The type of the state value.

      Parameters

      • initialValue: T

        The initial value of the state.

      • callbackfn: StateSideEffect<T>

        A function to run with the new and (optionally) previous value after each update.

      Returns [() => T, (newValue: T) => void]

      A tuple containing a getter and a setter for the state.

      const [getCount, setCount] = createState(0, (value, prev) => {
      console.log('Count changed from', prev, 'to', value);
      });
      setCount(1); // Logs: Count changed from 0 to 1
      console.log(getCount()); // 1