
State in url
State management and URL sync
Sync any JavaScript state to URL query parameters — objects, arrays, and dates. Fully typed, shareable, and persistent across reloads.
useUrlState — demo with remix.js
Try to type something
Reads from URL — no props, no context, types and structure are preserved
{name: stringage: undefinedundefinedagree_to_terms: falsebooleantags: []}
Instructions for:
Quick start
export const form: Form = {
name: '',
age: undefined,
agree_to_terms: false,
tags: [],
};
// use `Type` not `Interface`!
type Form = {
name: string;
age?: number;
agree_to_terms: boolean;
tags: { id: string; value: { text: string; time: Date } }[];
};import { useUrlState } from 'state-in-url/remix';
import { form } from './form';
export const ComponentA = () => {
// see docs for all possible params https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/remix/useUrlState
const { urlState, setUrl, setState } = useUrlState(form);
return <>
<input
id="name"
value={urlState.name}
onChange={(ev) => setUrl({ name: ev.target.value })}
/>
// OR can update state immediately but sync change to url as needed
<input
value={urlState.name}
onChange={(ev) => { setState(curr => ({ ...curr, name: ev.target.value })) }}
onBlur={() => setUrl()}
/>
<button onClick={() => setUrl((curr, initial) => initial)}>
Reset
</button>
</>
};import { useUrlState } from 'state-in-url/remix';
import { form } from './form';
export const ComponentB = () => {
const { urlState } = useUrlState(form);
// will be defaultValue from `form` if not in url, no need to check
return <div>name: {urlState.name}</div>
};import React from 'react';
import { useUrlState } from 'state-in-url/remix';
const form: Form={
name: '',
age: undefined,
agree_to_terms: false,
tags: [],
};
type Form = {
name: string;
age?: number;
agree_to_terms: boolean;
tags: {id: string; value: {text: string; time: Date } }[];
};
export const useFormState = () => {
const { urlState, setUrl: setUrlBase, reset } = useUrlState(form);
// first navigation will push new history entry
// all following will just replace that entry
// this way will have history with only 2 entries - ['/url', '/url?key=param']
const replace = React.useRef(false);
const setUrl = React.useCallback((
state: Parameters<typeof setUrlBase>[0],
opts?: Parameters<typeof setUrlBase>[1]
) => {
setUrlBase(state, { replace: replace.current, ...opts });
replace.current = true;
}, [setUrlBase]);
return { urlState, setUrl, resetUrl: reset };
};Why state-in-url?
URL state libraries exist, but most are either too cumbersome to set up or too limited in what they can store. state-in-url aims to be the one that just works.
state-in-url provides the useUrlState hook for Next.js and react-router. Store state without boilerplate, implement deep links, and share data between unrelated client components — no provider needed. API mirrors React.useState.
Structure and types are preserved, with full TypeScript support.
Code quality
Built with best practices and TDD — production-ready.
Other frameworks or pure JS
More hooks and helpers for serialization and decoding of data.
Check out the GitHub page — a star goes a long way.
Share with other devs
