subscribeWithSelector
How to subscribe to granular store updates in a store
subscribeWithSelector
subscribeWithSelector
middleware lets you subscribe to specific data based on current state.
const nextStateCreatorFn = subscribeWithSelector(stateCreatorFn)
Types
Signature
subscribeWithSelector<T>(stateCreatorFn: StateCreator<T, [], []>): StateCreator<T, [['zustand/subscribeWithSelector', never]], []>
Mutator
['zustand/subscribeWithSelector', never]
Reference
subscribeWithSelector(stateCreatorFn)
Parameters
stateCreatorFn
: A function that takesset
function,get
function andstore
as arguments. Usually, you will return an object with the methods you want to expose.
Returns
subscribeWithSelector
returns a state creator function.
Usage
Subscribing partial state updates
By subscribing to partial state updates, you register a callback that fires whenever the store's
partial state updates. We can use subscribe
for external state management.
import { createStore } from 'zustand/vanilla'
import { subscribeWithSelector } from 'zustand/middleware'
type PositionStoreState = { position: { x: number; y: number } }
type PositionStoreActions = {
setPosition: (nextPosition: PositionStoreState['position']) => void
}
type PositionStore = PositionStoreState & PositionStoreActions
const positionStore = createStore<PositionStore>()(
subscribeWithSelector((set) => ({
position: { x: 0, y: 0 },
setPosition: (position) => set({ position }),
})),
)
const $dot = document.getElementById('dot') as HTMLDivElement
$dot.addEventListener('mouseenter', (event) => {
const parent = event.currentTarget.parentElement
const parentWidth = parent.clientWidth
const parentHeight = parent.clientHeight
positionStore.getState().setPosition({
x: Math.ceil(Math.random() * parentWidth),
y: Math.ceil(Math.random() * parentHeight),
})
})
const render: Parameters<typeof positionStore.subscribe>[0] = (state) => {
$dot.style.transform = `translate(${state.position.x}px, ${state.position.y}px)`
}
render(positionStore.getInitialState(), positionStore.getInitialState())
positionStore.subscribe((state) => state.position, render)
const logger: Parameters<typeof positionStore.subscribe>[0] = (x) => {
console.log('new x position', { x })
}
positionStore.subscribe((state) => state.position.x, logger)
Here's the html
code
<div
id="dot-container"
style="position: relative; width: 100vw; height: 100vh;"
>
<div
id="dot"
style="position: absolute; background-color: red; border-radius: 50%; left: -10px; top: -10px; width: 20px; height: 20px;"
></div>
</div>
Troubleshooting
TBD