foxact
Version:
React Hooks/Utils done right. For browser, SSR, and React Server Components.
1 lines • 1.06 kB
JavaScript
require("client-only");var e=require("react"),n=require("../noop/index.cjs"),r=require("../no-ssr/index.cjs");const t=e=>e,a=(()=>{if("undefined"==typeof window)return e=>n.noop;let e=!1;const r=new Set,t=()=>{r.forEach(e=>e())};return n=>(r.add(n),e||(e=!0,window.addEventListener("hashchange",t)),()=>{r.delete(n)})})(),l=e=>"function"==typeof e;function o(){throw r.noSSRError("useUrlHashState cannot be used on the server without a serverValue")}exports.unstable_useUrlHashState=function(n,r){var s;let i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{raw:!1,serializer:t,deserializer:t};const u=i.raw?t:i.serializer,c=i.raw?t:i.deserializer,h=void 0===r?o:()=>u(r),d=e.useSyncExternalStore(a,()=>new URLSearchParams(location.hash.slice(1)).get(n),h),w=e.useMemo(()=>null===d?null!=r?r:null:c(d),[r,c,d]),S=e.useCallback(e=>{const t=location.hash,a=new URLSearchParams(t.slice(1)),o=l(e)?e(w):e;o===r||null===o?a.delete(n):a.set(n,u(o));const s=a.toString();t!==s&&(location.hash=s)},[r,w,n,u]);return[null!=(s=null!=w?w:r)?s:null,S]};
;