Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
use-location-state
Advanced tools
store and retrieve state into/from the browsers location history using modern hooks
useLocationState(name, defaultValue)
history.state
string | number | boolean | Date | Array | Object
useQueryState(name, defaultValue)
location.href
) and after navigation actions (back/forward)string | number | boolean | Date | string[]
yarn add use-location-state
Using react-router
or another popular router? For the best experience install one of the router integrations.
useLocationState()
and useQueryState()
work similar to the useState()
hook, as they also return the current value and a update function in a tuple [currentValue, updateValueFn]
.
The important difference is that you must pass a name before your default value for your state.
const [commentText, setCommentText] = useLocationState('commentText', '')
const [priceMax, setPriceMax] = useQueryState('priceMax', 30)
useLocationState()
is perfect, when you want to store a state that should not be reflected in the URL or in case of a complex data structure like a nested object/array.
const [commentText, setCommentText] = useLocationState('commentText', '')
The name you pass, in this case 'commentText'
, will be used as a key when storing the value. So when you use the same name (and default value) in another component, you will get the same state.
setCommentText('Wow, this works like a charm!')
The updated state will be restored when the pages reloads and after the user navigated to a new page and comes back using a back/forward action.
useQueryState()
is a great, when you want to store information about the current state of you app in the URL.
const [value, setValue] = useQueryState('itemName', 'default value')
The name you pass will be used as a parameter name in the query string, when setting a new value:
setValue('different value')
After calling the update function setValue()
with a new value, the state will be saved withing the query string of the browser, so that the new state is reproducable after reloads or history navigation (using forward / back button) or by loading the same URL anywhere else.
http://localhost:3000/#itemName=different+value
useQueryState() uses the browsers location.hash
property by default.
Check out the router integrations to use location.search
instead.
In cases where you want the updated state to be represented as a new entry in the history you can pass a options object to the set function, with the method property set to 'push'
.
setValue('a pushed value', { method: 'push' })
This changes the way this state change is handled when the user navigates. When the user now clicks the Back-Button, this state gets popped and the previous state is restored (instead of eg. navigating away).
import { useQueryState } from 'use-location-state'
function MyComponent() {
const [active, setActive] = useQueryState('active', true)
return (
<div>
<button type="button" onClick={() => setActive(!active)}>
Toggle
</button>
{active && <p>Some active content</p>}
</div>
)
}
import { useQueryState } from 'use-location-state'
function MyComponent() {
const [name, setName] = useQueryState('name', 'Sarah')
const [age, setAge] = useQueryState('age', 25)
const [active, setActive] = useQueryState('active', false)
// ...
}
In case you want use location.search
(after the question mark in the url) you need to use one of these extended versions of the package.
We plan to provide clean and easy-to-use integrations for all popular routers. At the moment we provide integrations for:
yarn add react-router-use-location-state
import {
useLocationState,
useQueryState,
} from 'react-router-use-location-state'
Usage works the same as described above, except that the URL will look like this now:
http://localhost:3000/?itemName=different+value
Gatsby & Reach Router are supported. Gatsby currently always scrolls up on location (state) changes. To keep the scroll position, when you update location state using the update function of useLocationState
, add these lines to the gatsby-browser.js file in gatsby root folder.
// keeps same scroll pos when history state is pushed/replaced (same location === same position)
// see: https://www.gatsbyjs.org/docs/browser-apis/#shouldUpdateScroll
exports.shouldUpdateScroll = ({ routerProps, getSavedScrollPosition }) => {
const currentPosition = getSavedScrollPosition(routerProps.location)
return currentPosition || true
}
Your favorite router is missing? Feel free to suggest a router.
Tested in current versions Chrome, Firefox, Safari, Edge, and IE11. This library relies on new, yet stable ECMAScript features, so you might need to include a polyfill if you want to support older browsers like IE11:
import 'react-app-polyfill/ie11'
import 'react-app-polyfill/stable'
FAQs
react hook to the browsers location query state
We found that use-location-state demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.