This module provides a custom React hook for sharing state between unrelated components in React applications.
A custom React hook that manages shared state across components.
defaultState: T
- An object representing the default state values._getInitial?: () => T
- Optional function to get the initial state, useful for SSR
An object containing:
state: T
- The current state.getState: () => T
- Function to get the current state.setState: T | Partial<T> | (T) => void
- Function to update the state.
import { useSharedState } from 'state-in-url/useSharedState';
const form = { name: '', age: 0 };
const { state, getState, setState } = useSharedState(form);
// Update state
setState({ name: 'test' });
// Or update state using a function
setState(curr => ({ ...curr, name: 'test' }));
// Get current state
const currentState = getState();
Updates the shared state.
value: T | ((currState: T) => T)
- New state value or a function that receives the current state and returns the new state.
Returns the current state.
- The current state object.