StoreType
import type { State } from 'zustand'
interface StoreType<StoreDataType> extends State {
name: string
data: StoreDataType
reset: () => void
update: (producer: (data: StoreDataType) => void) => void
}Unexpected error with integration github-files: Integration is not authenticated with GitHub
API reference
name
name name: stringName of the store to create.
Must be unique per store (ref).
data
data data: StoreDataTypeData you want to save in your store. Can be of any type. As the zustand doc states:
You can put anything in it: primitives, objects, functions.
reset
resetreset: () => voidFunction that, once invoked, resets the store's data to the value of the initialData provided to createStore()'s 2nd argument.
update
updateupdate: (producer: (data: StoreDataType) => void) => voidFunction that allows you to update your data. The current value is provided to the producer function and thanks to Immer: you don't have to care about immutability at all. Example:
const dealershipStore = createStore('dealership', { flagship: 'Ford Mustang' })
dealershipStpre.getState().update(data => {
data.flagship = 'Porsche Taycan'
})For convenience, we recommend extracting update() in a function once, and then using that function to perform changes. Example:
const dealershipStore = createStore('dealership', {
isOpened: true,
flagship: 'Ford Mustang',
})
const updateDealership = dealershipStore.getState().update
updateDealership(data => {
data.isOpened = false
})
updateDealership(data => {
data.isOpened = true
data.flagship = 'Porsche Taycan'
})Be careful to respect Immer's rules when it comes to producing a new state:
Last updated
Was this helpful?