state-in-url
Version:
Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-15 and react-router@6-7.
25 lines (24 loc) • 892 B
TypeScript
import { type JSONCompatible } from "../utils";
/**
* Custom React hook for sharing state between unrelated components.
*
* @param {T} defaultState - The default state object
* @param {() => T} [_getInitial] - Optional function to get initial state
* @returns {Object} Object containing `state`, `getState`, and `setState` functions
*
* * Example:
* ```ts
* export const form = { name: '', age: 0 };
* const { state, setState } = useSharedState(form);
*
* setState({ name: 'test' });
* // OR
* setState(curr => ({ ...curr, name: 'test' }))
* ```
* * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useSharedState}
*/
export declare function useSharedState<T extends JSONCompatible>(defaultState: T, _getInitial?: () => T): {
state: T;
getState: () => T;
setState: (value: Partial<T> | ((currState: T) => T)) => void;
};