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) • 428 B
JavaScript
import{encodeState as S}from"../encodeState/encodeState.mjs";import{getOtherParams as d}from"../utils.mjs";function s(t,e){const o=t.indexOf(e);return o===-1?[t,""]:[t.slice(0,o),t.slice(o+1)]}function l(t,e,o,f){const[i,n]=s(t,"#");const[a,m]=s(i,"?");const r=d(e,f);for(const[u,p]of new URLSearchParams(m)){r.set(u,p)}const c=S(o,e,r);const $=c?`?${c}`:"";const h=n?`#${n}`:"";return`${a}${$}${h}`}export{l as hrefWithState};