Estado tipado, vivendo na URL
useUrlState é o estado React que se grava na string de consulta. Objetos, arrays e datas mantêm seus tipos, cada estado é um link compartilhável e sobrevive a recarregamentos — sem providers, sem boilerplate.
- ~2 KB em gzip
- zero dependências
- TypeScript-first
- Next.js / react-router / Remix
- MIT
npm i state-in-urluseUrlState — ao vivo com react-router
Digite abaixo — veja a URL acender
Lê da URL — sem props, sem context, os tipos e a estrutura são preservados
{name: stringage: undefinedundefinedagree_to_terms: falsebooleantags: []}
A mesma API, três routers
Início rápido
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/react-router';
// for react-router v6
// import { useUrlState } from 'state-in-url/react-router6';
import { form } from './form';
export const ComponentA = () => {
// see docs for all possible params https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/react-router/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/react-router';
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/react-router';
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 };
};Por que state-in-url?
Existem bibliotecas de estado na URL, mas a maioria é complicada de configurar ou limitada no que pode armazenar. state-in-url pretende ser a que simplesmente funciona: uma API que espelha React.useState, com a URL como armazenamento.
Armazene estado sem boilerplate, construa links profundos e compartilhe dados entre componentes de cliente não relacionados — sem necessidade de provider. A estrutura e os tipos são preservados de ponta a ponta: um Date entra, um Date sai.
Construído com test-first, com suítes unitárias e e2e entre navegadores executando a cada commit.
Next.js: sem limite de Suspense
O hook nunca chama useSearchParams, então um componente que o usa não precisa ser envolvido em Suspense e não exclui sua página da pré-renderização — PPR e cacheComponents incluídos. Ele lê a URL diretamente e acompanha cada alteração posterior, incluindo um history.pushState de um código que nada sabe sobre ele.
Não usa Next.js ou react-router?
Os helpers encodeState / decodeState funcionam com qualquer framework ou JS puro — os hooks são uma conveniência por cima.
Confira a página no GitHub — uma estrela ajuda muito.
Compartilhe com outros desenvolvedores

