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) • 396 B
JavaScript
const c=new WeakMap;const n=new WeakMap;const o={get(t){return n.get(t)||[]},add(t,e){const s=this.get(t);n.set(t,[...s,e]);return()=>this.remove(t,e)},remove(t,e){const s=this.get(t);if(!s)return;const r=s.filter(i=>i!==e);if(r.length){n.set(t,r)}else{n.delete(t)}}};const a={get:function(t){return c.get(t)},set:function(t,e){c.set(t,e)}};exports.stateMap=a;exports.subscribers=o;
;