Gathering detailed insights and metrics for @umijs/use-params
Gathering detailed insights and metrics for @umijs/use-params
Gathering detailed insights and metrics for @umijs/use-params
Gathering detailed insights and metrics for @umijs/use-params
A React Hook to use URL query string as a state management
npm install @umijs/use-params
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6 Stars
135 Commits
1 Watching
2 Branches
1 Contributors
Updated on 12 Apr 2023
TypeScript (94.97%)
JavaScript (5.03%)
Cumulative downloads
Total Downloads
Last day
20.7%
20,785
Compared to previous day
Last week
5.9%
104,049
Compared to previous week
Last month
4.2%
427,969
Compared to previous month
Last year
54.8%
4,288,793
Compared to previous year
1
5
useUrlSearchParams()
A React Hook to use URL query string as a state management
npm install use-url-search-params
or
yarn add use-url-search-params
For most of the time you will do something like this:
1import React from "react"; 2import { useUrlSearchParams } from "use-url-search-params"; 3 4function App() { 5 // Your page URL will be like this by default: http://my.page?checked=true 6 const [params, setParams] = useUrlSearchParams({ checked: true }); 7 8 React.useEffect(() => { 9 // do something when `params.checked` is updated. 10 }, [params.checked]); 11 12 return ( 13 <div> 14 <input 15 type="checkbox" 16 checked={params.checked} 17 onChange={e => setParams({ checked: e.target.checked })} 18 /> 19 </div> 20 ); 21}
By default, all values parsed from URL query are string. In case you want to get boolean or number value, pass a second argument to useUrlSearchParams()
to specify data type you want to get from params
object. Here is an example:
1const initial = { 2 y: "option1" 3}; 4const types = { 5 x: Number, 6 y: Boolean, 7 z: Date, 8 t: ["option1", "option2", "option3"] 9}; 10const [params, setParams] = useUrlSearchParams(initial, types); 11 12// `params.x` will be number (or NaN) 13// `params.y` will be one of [undefined, true, false] 14// `params.z` will be instance of Date (can be Invalid Date) 15// `params.t` will be one of ["option1", "option2", "option3"] (can be `undefined` if not specified in `initial`)
Although you can use JSON.parse()
and JSON.stringify()
to get/set arbitrary serializable data to URL query, it is not recommended. URL query is a good place to store and persist page settings as key/value pairs such as table filter, sorting, paging, etc. We should keep it that way for simplicity. For complex data structure, you should consider using other state management for better performance, security and flexibility.
WARNING: Be aware of XSS attack. Be careful to validate values from URL query before using it by either using
types
- the second parameter passed touseUrlSearchParams()
or validate them yourself if neccessary.
But if you still insist, here is an example:
1function App() { 2 const [params, setParams] = useUrlSearchParams( 3 {}, 4 { 5 complexData: dataString => { 6 try { 7 return JSON.parse(dataString); 8 } catch (e) { 9 return {}; 10 } 11 } 12 } 13 ); 14 15 const onSetParams = data => { 16 setParams({ complexData: JSON.stringify(data) }); 17 }; 18 19 return <div>{/*...*/}</div>; 20}
Should just work with React Router or any routing system. Just make sure that your component re-render whenever route changes.
initial
(optional | Object): To set default values for URL query string.types
(optional | Object): Has similar shape with initial
, help to resolve values from URL query string. Supported types:
String
(default)Number
Bool
Date
- Date.prototype.toISOString()
is used to parse date to string, e.g date string in your URL query is zero UTC offsetThis library is built base on URLSearchParams interface
MIT
No vulnerabilities found.
No security vulnerabilities found.