donavon/use-persisted-state

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

JavaScript

1,369

32 commits

updated Apr 26, 2023

See the code

README

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 react@16.8.0 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):

This project follows the all-contributors specification. Contributions of any kind welcome!

custom-hook
hooks
localstorage
persistence
react
reactjs

donavon/use-persisted-state

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

JavaScript

1,369

32 commits

updated Apr 26, 2023

See the code

README

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 react@16.8.0 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):

This project follows the all-contributors specification. Contributions of any kind welcome!

custom-hook
hooks
localstorage
persistence
react
reactjs

Project health

Overall

2.7/10

Maintained

0/10

Code review

2/10
OpenSSF Scorecard

Latest release

0.3.3 Β· Nov 19, 2020

Releases this year

0

Significant stargazers

Max Stoiber

7,136 followers Β· starred Jul 2019

Ephraim Duncan

497 followers Β· starred Feb 2023

Rich Haines

254 followers Β· starred Mar 2021

Gildas Garcia

213 followers Β· starred Jan 2020