-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Difference between zustand and valtio
Daishi Kato edited this page Jul 11, 2021
·
4 revisions
Ref: https://github.com/pmndrs/zustand/issues/483
import create from 'zustand'
// intentionally show non-hook deep usage
const store = create(() => ({ obj: { count: 0 } }))
store.setState((prev) => ({ obj: { count: prev.obj.count + 1 } })
import { proxy } from 'valtio'
import state = proxy({ obj: { count: 0 } })
state.obj.count += 1
import create from 'zustand'
const useStore = create(() => ({
count1: 0,
count2: 1,
}))
const Component = () => {
const count1 = useStore((state) => state.count1) // manual render optimization with selector
// ...
}
import { proxy, useSnapshot } from 'valtio'
const state = proxy({
count1: 0,
count2: 0,
})
const Component = () => {
const { count1 } = useSnapshot(state) // automatic render optimization with property access
// ...
}