English · 简体中文 · 日本語 · 한국어 · Русский · Español · Português (BR) · Français · Tiếng Việt
This hook can be used as a base to create hooks for different routers
A custom React hook to create custom useUrlState hooks.
defaultState: object- An object representing the default state values.router: object- Router object withpushandreplacemethodsgetInitialState?: function- Optional function that return initial state.
An object containing:
state: object- The current state.getState: Function- Function to get state.updateState: Function- Function to update the state without updating the URL.updateUrl: Function- Function to update both the state and the URL.reset: Function- Function to reset state and url to default values
import { useUrlStateBase } from 'state-in-url/useUrlStateBase';
function useUrlStateCustom<T>(state: T) {
const router = React.useMemo({
push: (url: string) => window.history.pushState(url),
replace: (url: string) => window.history.replaceState(url)
}, []);
return useUrlState(state, router);
}Updates the state without modifying the URL.
value: T | Partial<T> | T => T- New state value or a function that receives the current state and returns the new state.
Updates both the state and the URL.
value?: T | Partial<T> | (currState: T) => T- Optional new state value or a function that receives the current state and returns the new state.options?: Options- Optional options object. Whenreplaceis true it will use router.replace. Other nextjs native options forrouter's push/replace.
Reset state and URL to default
options?: Options- Optional options object. Whenreplaceis true it will use router.replace. Other nextjs native options forrouter's push/replace.