3.0.0 • Published 3 years ago

@bytesoftio/local-store v3.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

@bytesoftio/local-store

Installation

yarn add @bytesoftio/local-store or npm install @bytesoftio/local-store

Table of contents

Description

This library is built on top of the @bytesoftio/store package and provides an integration with localStorage. This way your application state can survive page reloads, etc. Check out the docs of the other package to find a more in depth guide.

createLocalStore

Since @bytesoftio/store is used underneath, stores produced by this and the other package are interchangeable. A store created by createLocalStore can be used with useStore function from the @bytesoftio/use-store.

import React from "react"
import { createLocalStore } from "@bytesoftio/local-store"
import { useStore } from "@bytesoftio/use-store"

// state shared between components and services, cached in localStorage
const sharedAuthStore = createLocalStore("auth", { token: "abcde" })

const Component = () => {
  const authStore = useStore(sharedAuthStore)
  // local component state, created through an initializer function, cached in localStorage
  const counterStore = useStore(() => createLocalStore("counter", {count: 0}))

  const increment = () => counterStore.set({count: persistentState.count + 1})

  return (
    <div>
      <span>Auth token: {authStore.get().token}</span>
      <button onClick={increment}>{counterStore.get().count}</button>
    </div>
  )
}