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 state using modern hooks
string | number | boolean | string[]
yarn add use-location-state
In case you use react-router, please check out the router integration segment.
The useQueryState() works similar to the useState()
hook and returns the current value and a set function in a pair.
The important difference is that you need to pass a name before your default value for your state.
const [value, setValue] = useQueryState('itemName', 'default value')
The name you pass will be used in the query string store the state (after the state was changed).
setValue('different value')
After calling the set function 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).
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.
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>
)
}
Try it: Example in CodeSandbox
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)
// ...
}
Try it: Example in CodeSandbox
We plan to provide easy-to-use integrations for all major routers. (more routers soon - work in progress) At the moment we provide integrations for:
yarn add react-router-use-location-state
import { useQueryState } from 'react-router-use-location-state'
Your favorite router is missing? Feel free to suggest a router.
FAQs
react hook to the browsers location query state
The npm package use-location-state receives a total of 0 weekly downloads. As such, use-location-state popularity was classified as not popular.
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.