use-valtio-store
v1.1.0
Published
A React hook for managing state with Valtio, supporting persistent states using localStorage and sessionStorage, and optimized for server-side rendering.
Downloads
318
Maintainers
Readme
use-valtio-store
This React hook provides a way to manage state using Valtio, supporting persistent states with localStorage
and sessionStorage
.
Installation
Install:
npm install valtio use-valtio-store
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',
})