rewright
    Preparing search index...

    Function useReducer

    • A function for managing state, similar to useState. This function takes a reducer function, applying unique logic when setting the value of the managed state. The callback function triggered by this function processes an optional action parameter, allowing you to pass in values which influnce your reducer's update logic. The managed state is globally available.

      Parameters

      • name: string

        the name of the state to be managed.

      • callback: ReducerCallback

        the callback function to trigger when setting the value of the state.

      • OptionaldefaultValue: any

        optional; the default value of the state.

      Returns [StateGetter, ReducerCallback]

      A getter and setter function for the managed state.

      function foo() {
      const [getCounter, incCounter] = useReducer("counter", (incrementBy: number) => {
      return getCounter() + incrementBy;
      }, 2);

      incCounter(1); // Increments 'counter' by 1, counter = 3
      }