use-valtio-store
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

use-valtio-store

This React hook provides a way to manage state using Valtio, supporting persistent states with localStorage and sessionStorage.

Installation

  1. Install:

    npm install valtio use-valtio-store
  2. Usage

  • Basic
import { createStore } from 'use-valtio-store'

const initialState = {
  count: 0,
}

const store = createStore(initialState, {
  key: 'my-app-state', // optional
  storage: "localStorage" // optional
  onInit(state) { // optional
    console.log({ state })
  }
})
  • Class
import { createStore } from 'use-valtio-store'

class MyStore {
  count = 0

  get double() {
    return this.count * 2
  }

  inc() {
    this.count++
  }

  dec() {
    this.count--
  }
}

const store = createStore(new MyStore(), {
  key: 'my-app-state',
})

Package Sidebar

Install

npm i use-valtio-store

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

22.2 kB

Total Files

9

Last publish

Collaborators

  • onepercman