Coder Social home page Coder Social logo

edbrannin / use-persisted-state Goto Github PK

View Code? Open in Web Editor NEW

This project forked from segment-boneyard/use-persisted-state

0.0 1.0 0.0 546 KB

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

License: MIT License

JavaScript 100.00%

use-persisted-state's Introduction

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
Donavon West

๐Ÿš‡ โš ๏ธ ๐Ÿ’ก ๐Ÿค” ๐Ÿšง ๐Ÿ‘€ ๐Ÿ”ง ๐Ÿ’ป
Karol Majewski
Karol Majewski

๐Ÿ’ป

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

use-persisted-state's People

Contributors

donavon avatar allcontributors[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.