1.93 kBTypeScriptView Raw
1import { ControllableReducerAction, ControllableReducerParameters } from './useControllableReducer.types';
3 * The alternative to `React.useReducer` that lets you control the state from the outside.
4 *
5 * It can be used in an uncontrolled mode, similar to `React.useReducer`, or in a controlled mode, when the state is controlled by the props.
6 * It also supports partially controlled state, when some state items are controlled and some are not.
7 *
8 * The controlled state items are provided via the `controlledProps` parameter.
9 * When a reducer action is dispatched, the internal state is updated with the new values.
10 * A change event (`onStateChange`) is then triggered (for each changed state item) if the new state is different from the previous state.
11 * This event can be used to update the controlled values.
12 *
13 * The comparison of the previous and next states is done using the `stateComparers` parameter.
14 * If a state item has a corresponding comparer, it will be used to determine if the state has changed.
15 * This is useful when the state item is an object and you want to compare only a subset of its properties or if it's an array and you want to compare its contents.
16 *
17 * An additional feature is the `actionContext` parameter. It allows you to add additional properties to every action object,
18 * similarly to how React context is implicitly available to every component.
19 *
20 * @template State - The type of the state calculated by the reducer.
21 * @template Action - The type of the actions that can be dispatched.
22 * @template ActionContext - The type of the additional properties that will be added to every action object.
23 *
24 * @ignore - internal hook.
25 */
26export default function useControllableReducer<State extends {}, Action extends ControllableReducerAction, ActionContext = undefined>(parameters: ControllableReducerParameters<State, Action, ActionContext>): [State, (action: Action) => void];