27 lines
1.9 KiB
TypeScript
27 lines
1.9 KiB
TypeScript
import { ControllableReducerAction, ControllableReducerParameters } from './useControllableReducer.types';
|
|
/**
|
|
* The alternative to `React.useReducer` that lets you control the state from the outside.
|
|
*
|
|
* 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.
|
|
* It also supports partially controlled state, when some state items are controlled and some are not.
|
|
*
|
|
* The controlled state items are provided via the `controlledProps` parameter.
|
|
* When a reducer action is dispatched, the internal state is updated with the new values.
|
|
* A change event (`onStateChange`) is then triggered (for each changed state item) if the new state is different from the previous state.
|
|
* This event can be used to update the controlled values.
|
|
*
|
|
* The comparison of the previous and next states is done using the `stateComparers` parameter.
|
|
* If a state item has a corresponding comparer, it will be used to determine if the state has changed.
|
|
* 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.
|
|
*
|
|
* An additional feature is the `actionContext` parameter. It allows you to add additional properties to every action object,
|
|
* similarly to how React context is implicitly available to every component.
|
|
*
|
|
* @template State - The type of the state calculated by the reducer.
|
|
* @template Action - The type of the actions that can be dispatched.
|
|
* @template ActionContext - The type of the additional properties that will be added to every action object.
|
|
*
|
|
* @ignore - internal hook.
|
|
*/
|
|
export declare function useControllableReducer<State extends {}, Action extends ControllableReducerAction, ActionContext = undefined>(parameters: ControllableReducerParameters<State, Action, ActionContext>): [State, (action: Action) => void];
|