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
Typescript
Module System
98.3
Supply Chain
99.5
Quality
82.5
Maintenance
100
Vulnerability
100
License
TypeScript (94.97%)
JavaScript (5.03%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
12,903,550
Last Day
6,596
Last Week
120,228
Last Month
503,968
Last Year
5,723,141
MIT License
6 Stars
135 Commits
2 Branches
1 Contributors
Updated on Apr 12, 2023
Latest Version
1.0.9
Package Id
@umijs/use-params@1.0.9
Unpacked Size
18.13 kB
Size
5.74 kB
File Count
10
Published on
Nov 22, 2021
Cumulative downloads
Total Downloads
Last Day
-13.5%
6,596
Compared to previous day
Last Week
-1.8%
120,228
Compared to previous week
Last Month
-13.1%
503,968
Compared to previous month
Last Year
54.1%
5,723,141
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.