Store

Simple store that can be hooked into React.

Source code is hosted on GitHub

yarn add @corets/store

Seamless React integration is shipped in a separate package:

useStore

There is a version of this package that syncs directly to the localStorage:

Local Storage Store

createStore()

Creates a new instance of observable store:

import { createStore } from "@corets/store"

const store = createStore({ some: "data" })

Create a new instance without the factory function:

import { Store } from "@corets/store"

const store = new Store({ some: "data" })

Create a new instance with a custom differ:

import { createStore } from "@corets/store"

const differ = (oldValue, newValue) => true
const store = createStore({ some: "data" }, { differ })

Store.get()

Retrieve everything from the store:

store.get()

Store.set()

Replace everything in the store:

store.set({ foo: "bar" })

Store.put()

Update store by merging new and old state:

store.put({ some: "data" })

Store.listen()

Listen to changes:

store.listen((value) => console.log(value))

Listen to changes with a custom differ:

const differ = (oldValue, newValue) => true

store.listen((value) => console.log(value), { differ })

Listen to a subset of data, using a custom mapper:

const mapper = {value} => ({  })

store.listen((value) => console.log(value), { mapper })

Store.use()

Convenience method for people used to React's useState syntax:

import { createStore } from "@corets/store"

const [store, setStore] = createStore({ foo: "bar" }).use()

Last updated