GithubHelp home page GithubHelp logo

thisisrajat / react-localstorage-sync Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 2.0 96 KB

A higher order component that sync localStorage and components state ๐Ÿ’พ

License: MIT License

JavaScript 100.00%
react higher-order-component localstorage localstorage-api sync setstate react-dom localstorage-components persistent-storage

react-localstorage-sync's Introduction

๐Ÿ”ฅ react-localstorage-sync ๐Ÿ”ฅ

This is a small utility that will help you solve your applications data persistence needs.

At the heart this is a higher order component which hooks itself to the Component that is wrapped in it.

Examples ๐ŸŽ‰

You have a component with an implementation like this:

  import React, { Component } from 'react';

  class SomeComponent extends Component {
    state = {
      tick: 1,
      tock: 1,
    }

    increaseStateCount = (keyName) => {
      this.setState({
        [keyName]: this.state[keyName] + 1,
      });
    }

    render() {
      const { tick, tock } = this.state;
      return (
        <div>
          <div
            onClick={this.increaseStateCount.bind(this, 'tick')}
          >
            Tick is {tick}
          </div>

          <div
            onClick={this.increaseStateCount.bind(this, 'tock')}
          >
            Tock is {tock}
          </div>
        </div>
      );
    }
  }

  export default SomeComponent;

You can quickly add persistence to the state by following 4 steps:

  1. Import withLocalStorage and wrap it on your component.
  import withLocalStorage from 'react-localstorage-sync';
  ...
  SomeComponent = withLocalStorage(SomeComponent);
  1. Tell which state keys to persist in the localStorage and under what namespace
  SomeComponent.localStorageNamespace = 'SomeComponent';

  SomeComponent.localStorageKeys = ['tick', 'tock'];
  1. Get saved state from localStorage when component mounts.
  componentDidMount() {
    this.setState(this.props.getStateFromLocalStorage(this));
  }
  1. Dance. ๐Ÿ‘ฏ๐Ÿ‘ฏโ€โ™‚๏ธ

Your component now looks like this:

  import React, { Component } from 'react';
  import withLocalStorage from 'react-localstorage-sync';

  class SomeComponent extends Component {
    state = {
      tick: 1,
      tock: 1,
    }

    componentDidMount() {
      this.setState(this.props.getStateFromLocalStorage(this));
    }

    increaseStateCount = (keyName) => {
      this.setState({
        [keyName]: this.state[keyName] + 1,
      });
    }

    render() {
      const { tick, tock } = this.state;
      return (
        <div>
          <div
            onClick={this.increaseStateCount.bind(this, 'tick')}
          >
            Tick is {tick}
          </div>

          <div
            onClick={this.increaseStateCount.bind(this, 'tock')}
          >
            Tock is {tock}
          </div>
        </div>
      );
    }
  }

  SomeComponent.localStorageKeys = ['tick', 'tock'];
  SomeComponent.localStorageNamespace = 'SomeComponent';

  SomeComponent = withLocalStorage(SomeComponent);

  export default SomeComponent;

react-localstorage-sync's People

Contributors

thisisrajat avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

thomas4g

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.