Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Index

Type aliases

Functions

Type aliases

CreateLocalStore

CreateLocalStore: <TValue>(storageKey: string, initialValue: TValue) => ObservableStore<TValue>

Type declaration

    • <TValue>(storageKey: string, initialValue: TValue): ObservableStore<TValue>
    • Type parameters

      • TValue: object

      Parameters

      • storageKey: string
      • initialValue: TValue

      Returns ObservableStore<TValue>

Functions

Const createLocalStore

  • createLocalStore<TValue>(storageKey: any, initialValue: any): ObservableStore<TValue>
  • Type parameters

    • TValue: object

    Parameters

    • storageKey: any
    • initialValue: any

    Returns ObservableStore<TValue>