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

2 lines (1 loc) 371 B
const o=new WeakMap;const n=new WeakMap;const a={get(t){return n.get(t)||[]},add(t,e){const s=this.get(t);n.set(t,s.concat(e));return()=>this.remove(t,e)},remove(t,e){const s=this.get(t);const c=s.filter(r=>r!==e);if(c.length){n.set(t,c)}else{n.delete(t)}}};const i={get:function(t){return o.get(t)},set:function(t,e){o.set(t,e)}};export{i as stateMap,a as subscribers};