Gathering detailed insights and metrics for use-url-sync
Gathering detailed insights and metrics for use-url-sync
Gathering detailed insights and metrics for use-url-sync
Gathering detailed insights and metrics for use-url-sync
⚛️ use-url-sync is a utility package that helps you sync your states to url without hassle
npm install use-url-sync
Typescript
Module System
Node Version
NPM Version
61.7
Supply Chain
91.4
Quality
75.6
Maintenance
100
Vulnerability
100
License
TypeScript (96.21%)
JavaScript (3.79%)
Total Downloads
2,124
Last Day
1
Last Week
5
Last Month
43
Last Year
281
MIT License
2 Stars
63 Commits
1 Watchers
9 Branches
1 Contributors
Updated on Jun 04, 2022
Minified
Minified + Gzipped
Latest Version
1.3.0
Package Id
use-url-sync@1.3.0
Unpacked Size
11.51 kB
Size
3.87 kB
File Count
11
NPM Version
8.8.0
Node Version
16.15.0
Cumulative downloads
Total Downloads
Last Day
-75%
1
Compared to previous day
Last Week
-50%
5
Compared to previous week
Last Month
22.9%
43
Compared to previous month
Last Year
-39.4%
281
Compared to previous year
use-url-sync is a set of tools that help you sync your state to URL. Along with this package, you can do some awesome stuff, such as:
useUrlState
to get value from current page pathuseUrlSync
or getUrlString
Let's say that the path is /user?name=andy&page=1&isEmployee=false
1import React from 'react'; 2import { useLocation } from 'react-router-dom'; 3import { useSyncUrl, useUrlState } from 'use-url-sync'; 4 5const App = () => { 6 const location = useLocation(); 7 8 /* Retrieve value from URL */ 9 const [page, setPage] = useUrlState({ 10 name: 'page', 11 defaultValue: 0, 12 onExists: p => parseInt(p, 10) 13 }); 14 const [name, setName] = useUrlState({ 15 name: 'name', 16 defaultValue: '' 17 }); 18 const [isEmployee, setIsEmployee] = useUrlState({ 19 name: 'isEmployee', 20 defaultValue: false, 21 onExists: v => v === 'true' 22 }); 23 24 /* Sync states to path */ 25 useUrlSync( 26 { 27 states: { 28 name, 29 page, 30 isEmployee: `${isEmployee}` 31 }, 32 /* Ignored conditions */ 33 ignore: { 34 page: p => p === 0 35 } 36 }, 37 nextPath => { 38 location.replace(nextPath); 39 } 40 ); 41 42 const incrementPage = () => setPage(p => p + 1); 43 44 const decrementPage = () => setPage(p => p - 1); 45 46 const toggleEmployeeStatus = () => { 47 setIsEmployee(v => !v); 48 }; 49 50 return ( 51 <> 52 <div id="page-togglers"> 53 <button type="button" id="increase-page" onClick={incrementPage}> 54 + 1 55 </button> 56 <button type="button" id="decrease-page" onClick={decrementPage}> 57 - 1 58 </button> 59 </div> 60 <button type="button" id="toggle-status" onClick={toggleEmployeeStatus}> 61 Toggle Employee Status 62 </button> 63 <p id="current-page">{page}</p> 64 <p id="current-name">{name}</p> 65 <p id="current-isEmployee">{isEmployee ? 'true' : 'false'}</p> 66 </> 67 ); 68};
1npm install use-url-sync
or
1yarn add use-url-sync
This package contains an index.d.ts
type definition file, so you can use it in TypeScript without extra configuration.
1 ... 2 3 const [page, setPage] = useUrlState<number>({ 4 name: 'page', 5 defaultValue: 0, 6 onExists: (p: any) => parseInt(p, 10) 7 }); 8 const [name, setName] = useUrlState<string>({ 9 name: 'name', 10 defaultValue: '' 11 }); 12 13 interface IStates { 14 page: number; 15 name: string; 16 } 17 18 useUrlSync<IStates>( 19 { 20 states: { 21 page, 22 name 23 }, 24 ignore: { 25 page: (p: number) => p === 0 26 } 27 }, 28 (nextPath: string) => { 29 location.replace(nextPath); 30 } 31 ); 32 33 ...
MIT License
Copyright (c) 2021 Ilham-Pratama
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
No security vulnerabilities found.