@kimaramyz/use-query-params
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
export * from '../types/index' | ||
export * from './types/index' |
@@ -1,3 +0,3 @@ | ||
import { useState as f, useEffect as d, useCallback as l } from "react"; | ||
const w = (t) => t == null, p = () => ({ | ||
import { useState as f, useEffect as d, useCallback as w } from "react"; | ||
const l = (t) => t == null, p = () => ({ | ||
isShallow: !1 | ||
@@ -9,7 +9,7 @@ }); | ||
const [r, n] = f(window.location.search), a = (o, u = null) => { | ||
const s = w(u) || t.isShallow; | ||
const s = l(u) || t.isShallow; | ||
window.history[s ? "replaceState" : "pushState"]( | ||
u, | ||
"", | ||
`${window.location.pathname}${w(o) ? "" : o}` | ||
`${window.location.pathname}${l(o) ? "" : o}` | ||
), window.dispatchEvent(new Event("popstate")); | ||
@@ -24,3 +24,3 @@ }, i = () => { | ||
function m(t = p()) { | ||
const [r, n] = h(t), a = l( | ||
const [r, n] = h(t), a = w( | ||
(s) => { | ||
@@ -33,3 +33,3 @@ const e = new URLSearchParams( | ||
[] | ||
), o = l((s) => { | ||
), o = w((s) => { | ||
const e = {}, c = new URLSearchParams(s).entries(); | ||
@@ -39,5 +39,5 @@ for (const [y, S] of c) | ||
return e; | ||
}, [])(r), u = l( | ||
}, [])(r), u = w( | ||
(s) => { | ||
if (w(s)) | ||
if (l(s)) | ||
n(s, null); | ||
@@ -66,2 +66,4 @@ else { | ||
export { | ||
p as defaultOptions, | ||
l as isNullType, | ||
g as useQueryParam, | ||
@@ -68,0 +70,0 @@ m as useQueryParams, |
@@ -1,1 +0,1 @@ | ||
(function(n,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],e):(n=typeof globalThis<"u"?globalThis:n||self,e(n.useQueryParams={},n.React))})(this,function(n,e){"use strict";const d=s=>s==null,f=()=>({isShallow:!1});function y(s=f()){if(!window)throw new ReferenceError("'window' is undefined.");const[i,r]=e.useState(window.location.search),u=(a,l=null)=>{const o=d(l)||s.isShallow;window.history[o?"replaceState":"pushState"](l,"",`${window.location.pathname}${d(a)?"":a}`),window.dispatchEvent(new Event("popstate"))},c=()=>{r(window.location.search)};return e.useEffect(()=>(window.addEventListener("popstate",c),()=>{window.removeEventListener("popstate",c)}),[]),[i,u]}function p(s=f()){const[i,r]=y(s),u=e.useCallback(o=>{const t=new URLSearchParams(o).toString();return t.length>0?`?${t}`:t},[]),a=e.useCallback(o=>{const t={},w=new URLSearchParams(o).entries();for(const[m,Q]of w)t[m]=Q;return t},[])(i),l=e.useCallback(o=>{if(d(o))r(o,null);else{const t=JSON.parse(JSON.stringify(o)),w=Object.keys(t).length>0;r(u(t),w?t:null)}window.dispatchEvent(new Event("popstate"))},[r]);return e.useEffect(()=>{r(i)},[i]),[a,l]}function S(s,i=f()){const[r,u]=p(i),c=a=>{u({...r,[s]:a})};return[r[s],c]}n.useQueryParam=S,n.useQueryParams=p,n.useQueryString=y,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
(function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e.useQueryParams={},e.React))})(this,function(e,n){"use strict";const l=s=>s==null,d=()=>({isShallow:!1});function y(s=d()){if(!window)throw new ReferenceError("'window' is undefined.");const[o,r]=n.useState(window.location.search),a=(u,f=null)=>{const i=l(f)||s.isShallow;window.history[i?"replaceState":"pushState"](f,"",`${window.location.pathname}${l(u)?"":u}`),window.dispatchEvent(new Event("popstate"))},c=()=>{r(window.location.search)};return n.useEffect(()=>(window.addEventListener("popstate",c),()=>{window.removeEventListener("popstate",c)}),[]),[o,a]}function p(s=d()){const[o,r]=y(s),a=n.useCallback(i=>{const t=new URLSearchParams(i).toString();return t.length>0?`?${t}`:t},[]),u=n.useCallback(i=>{const t={},w=new URLSearchParams(i).entries();for(const[m,Q]of w)t[m]=Q;return t},[])(o),f=n.useCallback(i=>{if(l(i))r(i,null);else{const t=JSON.parse(JSON.stringify(i)),w=Object.keys(t).length>0;r(a(t),w?t:null)}window.dispatchEvent(new Event("popstate"))},[r]);return n.useEffect(()=>{r(o)},[o]),[u,f]}function S(s,o=d()){const[r,a]=p(o),c=u=>{a({...r,[s]:u})};return[r[s],c]}e.defaultOptions=d,e.isNullType=l,e.useQueryParam=S,e.useQueryParams=p,e.useQueryString=y,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); |
@@ -11,3 +11,3 @@ { | ||
}, | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"keywords": [ | ||
@@ -38,4 +38,3 @@ "react", | ||
"files": [ | ||
"dist", | ||
"types" | ||
"dist" | ||
], | ||
@@ -48,6 +47,12 @@ "publishConfig": { | ||
}, | ||
"tsd": { | ||
"directory": "src" | ||
}, | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
"preview": "vite preview", | ||
"test": "vitest", | ||
"test-types": "tsd", | ||
"clean": "rimraf dist" | ||
}, | ||
@@ -60,2 +65,3 @@ "devDependencies": { | ||
"react-dom": "^18.2.0", | ||
"tsd": "^0.24.1", | ||
"typescript": "^4.6.4", | ||
@@ -62,0 +68,0 @@ "vite": "^3.1.0", |
# @kimaramyz/use-query-params | ||
`@kimaramyz/use-query-params` is a library of React hooks for using URL query params as state. Light-weight, TS support and no dependencies. This allows you to easily encode and decode data of primitive type. Implemented by `History` API and `URLSearchParams` API. | ||
`@kimaramyz/use-query-params` is a library of React hooks for using URL query params as state. Light-weight, TS support and no dependencies. This allows you to easily synchronize(encode and decode) react state with URL query parameters. Implemented by `History` API and `URLSearchParams` API. | ||
@@ -17,3 +17,3 @@ ## Background | ||
- Provides three options of URL query hooks | ||
- Provides three options for using query params. | ||
@@ -20,0 +20,0 @@ - [useQueryParams](#useQueryParams) |
11736
2.21%102
3.03%10
11.11%