How Zustand's SetState Leverages Closures In JavaScript
Zustand's setState leverages closures in JavaScript & arrow functions. It accepts partial & replace params, updating state using Object.assign when necessary.
In this article, I will provide a review on how setState in Zustand’s source code is written/works. This concept leverages closures in JavaScript and arrow functions.
StoreApi type is straight forward.
export interface StoreApi<T> {
setState: SetStateInternal<T>
getState: () => T
getInitialState: () => T
subscribe: (listener: (state: T, prevState: T) => void) => () => void
}
setState accepts two parameters
partial
replace
Let’s perform an experiment using the example demo app provided in theZustand repo.
I added some console statements in the dist to see...