UNPKG

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, react-router@6-7, and remix@2.

2 lines (1 loc) 434 B
"use strict";var l=require("../encodeState/encodeState.js");var m=require("../utils.js");function o(e,r){const t=e.indexOf(r);return t===-1?[e,""]:[e.slice(0,t),e.slice(t+1)]}function q(e,r,t,a){const[i,c]=o(e,"#");const[u,f]=o(i,"?");const n=m.getOtherParams(r,a);for(const[S,d]of new URLSearchParams(f)){n.set(S,d)}const s=l.encodeState(t,r,n);const h=s?`?${s}`:"";const $=c?`#${c}`:"";return`${u}${h}${$}`}exports.hrefWithState=q;