Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

React - Get and set local and session storage easily using the Context API

Notifications You must be signed in to change notification settings

giannif/react-storage-context

Repository files navigation

react-storage-context

NPM JavaScript Style Guide Coverage Status

Get and set local and session storage using the React Context API

Install

yarn add react-storage-context

Usage

StorageContext

Initializes and manages storage, setting the values of the StorageContext.Provider

import StorageContext from "react-storage-context"

@StorageContext("your-storage-id")
class App extends PureComponent {
  render() {
    // children that will read and write storage
  }
}

withStorageContext

Will provide the api as props:

  • local local storage object
  • session session storage object
  • setLocal update local storage object
  • setSession update session storage object
import { withStorageContext } from "react-storage-context"

@withStorageContext
class App extends PureComponent {
  render() {
    // props to read
    const {
      local: { someLocalVal },
      session
    } = this.props
    // props to invoke save methods
    const { saveLocal, saveSession } = this.props
    return <div>{someLocalVal}</div>
  }
}

Consumer

import { Consumer } from "react-storage-context"

class App extends PureComponent {
  render() {
    // render props
    return (
      <Consumer>{({ session }) => <div>Session storage values: {JSON.stringify(session, undefined, 4)}</div>}</Consumer>
    )
  }
}

License

MIT © giannif

About

React - Get and set local and session storage easily using the Context API

Resources

Stars

Watchers

Forks

Packages

No packages published