UNPKG

react-auto-controlled

Version:

Component autonomous state control utilities in React class methods and React Hooks

34 lines (33 loc) 1.54 kB
/// <reference types="react" /> /** * Returns a stateful value, and a function to update it, but only if no prop has been provided. * * Mimics the `useState()` React Hook signature, but returns additional utility methods for * automatically deriving state from prop. * * @export * @template State :: The state type, which can be anything. * @param {State} initialState The initial state of the state modifier hook, internal to the component. * @param {Props} [props={}] (optional) Prop values to take control of the state and initial state, if provided. * @param {{ prop?: State; defaultProp?: State; }} [props={}] * @returns {[ * State, * React.Dispatch<React.SetStateAction<State>>, * React.Dispatch<React.SetStateAction<State>>, * () => void * ]} A list made up of the state and the methods, in the form of `[ state, setState, trySetState, getDerivedStateFromProp ]`. */ export declare const useAutoControlled: <State>(initialState: State, props?: { /** * Controls the state on every render, including the initial state. Use this if you need external control. * * @type {State} */ prop?: State | undefined; /** * Controls the initial state. Use this if your uncontrolled component accepts a custom starting value. * * @type {State} */ defaultProp?: State | undefined; }) => [State, import("react").Dispatch<import("react").SetStateAction<State>>, import("react").Dispatch<import("react").SetStateAction<State>>, () => void];