All Projects → donavon → Use Persisted State

donavon / Use Persisted State

Licence: mit
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Use Persisted State

mst-persist
Persist and hydrate MobX-state-tree stores (in < 100 LoC)
Stars: ✭ 75 (-92.05%)
Mutual labels:  persistence, localstorage
Vuex Localstorage
Persist Vuex state with expires by localStorage or some else storage.
Stars: ✭ 129 (-86.32%)
Mutual labels:  persistence, localstorage
Local Storage
React hook which syncs localStorage[key] with the comp.
Stars: ✭ 402 (-57.37%)
Mutual labels:  hooks, localstorage
React Storage Hooks
React hooks for persistent state
Stars: ✭ 146 (-84.52%)
Mutual labels:  hooks, localstorage
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (-23.97%)
Mutual labels:  persistence, localstorage
Base Mvvm
App built to showcase basic Android View components like ViewPager, RecyclerView(homogeneous and heterogeneous items), NavigationDrawer, Animated Vector Drawables, Collapsing Toolbar Layout etc. housed in a MVVM architecture
Stars: ✭ 18 (-98.09%)
Mutual labels:  persistence
Broadcast Channel
📡 BroadcastChannel to send data between different browser-tabs or nodejs-processes 📡
Stars: ✭ 843 (-10.6%)
Mutual labels:  localstorage
Storagedb
MongoDB-like API for HTML5 Storage (localStorage and sessionStorage)
Stars: ✭ 16 (-98.3%)
Mutual labels:  localstorage
Web3 React
🧰 A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
Stars: ✭ 788 (-16.44%)
Mutual labels:  hooks
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-98.52%)
Mutual labels:  hooks
Fisher
Simple yet powerful webhooks catcher
Stars: ✭ 11 (-98.83%)
Mutual labels:  hooks
Genesis Simple Hook Guide
WordPress plugin that displays names of all Genesis hooks on the current page dynamically.
Stars: ✭ 25 (-97.35%)
Mutual labels:  hooks
Usestatewithlayoutanimation
Abstraction for `React Native`'s `LayoutAnimation` and `useState`
Stars: ✭ 19 (-97.99%)
Mutual labels:  hooks
Aframe Persist Component
Persisting Aframe attribute data using localStorage
Stars: ✭ 10 (-98.94%)
Mutual labels:  localstorage
Rooks
Essential hooks ⚓ to super charge your components!
Stars: ✭ 889 (-5.73%)
Mutual labels:  hooks
Axios Hooks
🦆 React hooks for axios
Stars: ✭ 862 (-8.59%)
Mutual labels:  hooks
React Latest Framework
a client framework of React
Stars: ✭ 835 (-11.45%)
Mutual labels:  hooks
Awesome React Hooks
A curated list about React Hooks
Stars: ✭ 932 (-1.17%)
Mutual labels:  hooks
React Native Localstorage
Manage LocalStorage items in a web environment
Stars: ✭ 11 (-98.83%)
Mutual labels:  localstorage
Flagchecker
For effective cheating detection in competitions. Utilizes Linux Kernel Module (LKM) for generating flags.
Stars: ✭ 24 (-97.45%)
Mutual labels:  hooks

use-persisted-state

A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.

npm version All Contributors

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.

Features

💾 Persists the state to localStorage

🖥 Syncs between tabs and/or browser windows

📑 Shares state w/multiple hooks on a page

Requirement

To use use-persisted-state, you must use [email protected] or greater which includes Hooks.

Installation

$ npm i use-persisted-state

Example

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.

License

MIT Licensed

Contributors

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].