@bytesoftio/local-store
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@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>
  )
}

Readme

Keywords

none

Package Sidebar

Install

npm i @bytesoftio/local-store

Weekly Downloads

1

Version

3.0.0

License

MIT

Unpacked Size

4.6 kB

Total Files

10

Last publish

Collaborators

  • maximkott