Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
use-hash-state
Advanced tools
Hook to read and write React state from the URL hash
// https://my-app#{hello:"world"}
const {
state: { hello },
} = useHashState({});
// hello === "world"
For simple demos and proofs of concepts, it often suffices to store state in the client, without a database.
URLs are a good place to store data:
This hook is not a router, it merely synchronizes the state within a component with state stored in the URL.
I would not use this for any production system, but it's invaluable for sharing ideas with colleagues.
npm install --save use-hash-state
const { state, setStateAtKey } = useHashState(initialState, options);
initialState
(type: {}
)
The state to start with, also used to determine what is considered a valid URL state by default (see the validateKeysAndTypes
option below for more).
options
See Options argument below
state
The current state, preferentially taken from the URL.
setStateAtKey
(type: (key: keyof T, value: any) => void
)
Updates the state at a particular key, also updating the URL.
In a simple counter component:
import React from 'react';
import useHashState from 'use-hash-state';
const Counter = () => {
const initialState = {
count: 0,
};
const { state, setStateAtKey } = useHashState(initialState);
const handleIncrement = () => {
setStateAtKey('count', state.count + 1);
};
return (
<>
<span>Count: {state.count}</span>
<button onClick={handleIncrement}>Increment</button>
</>
);
};
Upon load, the URL hash will be set to the URL-encoded JSON representation of the initial state:
http://localhost:1234/#%7B%22count%22%3A2%7D
As Increment is clicked, setStateAtKey
updates the state within the component, and the URL will be updated to reflect the internal state.
Reloading or sharing the link will cause the initialState
to be ignored, in favor of the URL state.
Options may be passed as the second argument to useHashState
:
import useHashState, { UseHashStateOptions } from 'use-hash-state';
const MyComponent = () => {
const initalState = {};
const options: UseHashStateOptions = {
usePushState: true, // boolean | undefined
validateKeysAndTypes: true, // boolean | undefined
customValidator: (obj: {}) => {
return true;
}, // (obj: {}) => boolean | undefined
};
const { state, setStateAtKey } = useHashState(initialState, options);
// ...
};
usePushState
(type: boolean | undefined
, default: false
)
By default, useHashState
uses history.replaceState
to update the URL. This means that the back and forward buttons will ignore the URL updates made by this hook, which is suitable for quickly-updating state when you do not want a massively long history.
If you set usePushState
to true
, the hook will use history.pushState
, so every state update will add a new history entry, and you will be able to use the browser next / previous buttons to access previous states.
validateKeysAndTypes
(type: boolean | undefined
, default: true
)
When validateKeysAndTypes
is set to true
(the default), the hook will check that any object in the URL has the same keys as the initialState
that you provide, and that the values at those keys are also the same types as in initialState
. If the URL state is not the same shape, it will be ignored, and the initial state will be applied.
This is handy for ensuring that your component state does not get super crazy because of some malformed URLs, particularly when you are developing and changing the shape of your state often.
customValidator
(type: (obj: {}) => boolean | undefined
, default: undefined
)
If you have a more complicated state that you'd like to validate, provide a customValidator
, which will be passed the JSON object parsed from the URL. Return true
if that object should be read into the local state, false
otherwise.
This is useful for when you want to ensure that values have certain bounds.
Note that this is applied in addition to basic key and type validation if validateKeysAndTypes
is set to true
.
MIT
FAQs
Store application state in the URL hash
The npm package use-hash-state receives a total of 1 weekly downloads. As such, use-hash-state popularity was classified as not popular.
We found that use-hash-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.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.