![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
use-persisted-state
Advanced tools
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
use-persisted-state
is not a hook itself, but is a factory that accepts a storage key
and an optional storage provider (default = localStorage
) and returns a hook
that you can use as a direct replacement for useState
.
💾 Persists the state to localStorage
🖥 Syncs between tabs and/or browser windows
📑 Shares state w/multiple hooks on a page
To use use-persisted-state
, you must use react@16.8.0
or greater which includes Hooks.
$ npm i use-persisted-state
Let's take a look at how you can use use-persisted-state
.
Here we have an example of a typical up/down counter.
import { useState } from 'react';
const useCounter = initialCount => {
const [count, setCount] = useState(initialCount);
return {
count,
increment: () => setCount(currentCount => currentCount + 1),
decrement: () => setCount(currentCount => currentCount - 1),
};
};
export default useCounter;
Let's replace the import of react
with an import from use-persisted-state
.
And we'll call createPersistedState
(the factory function).
This will return a useCounterState
hook that we can use in place of useState
.
The complete code is as follows.
import createPersistedState from 'use-persisted-state';
const useCounterState = createPersistedState('count');
const useCounter = initialCount => {
const [count, setCount] = useCounterState(initialCount);
return {
count,
increment: () => setCount(currentCount => currentCount + 1),
decrement: () => setCount(currentCount => currentCount - 1),
};
};
export default useCounter;
The state is shared with any other hook using the same key, either on the same page, across tabs, or even browser windows.
For example, open two copies of your app in two tabs or even two windows. Any changes to state in one tab will be rendered on the other tab.
You can also close the browser and the next time you run your app, the state will be rendered as it was before you closed your browser.
MIT Licensed
Thanks goes to these wonderful people (emoji key):
Donavon West 🚇 ⚠️ 💡 🤔 🚧 👀 🔧 💻 | Karol Majewski 💻 | Octave Raimbault 💻 | Dennis Morello 💻 | Florent 💻 | Mark Adamson 💻 | Vitor Dino 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
The npm package use-persisted-state receives a total of 0 weekly downloads. As such, use-persisted-state popularity was classified as not popular.
We found that use-persisted-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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.