Core concepts, definitions, basic syntax, and first principles expected in round 1 screening.
Answer: Zustand (German for "state") is a lightweight, fast, and scalable state management library for React. It is built on a simplified Flux-like architecture but designed around hooks as the primary interface.
Core Philosophies:
<Provider> tags. Stores are plain JavaScript objects containing state and actions, and hooks can be imported and consumed directly in any React component.Answer:
| State Solution | Boilerplate Level | Render Performance | Complexity |
|---|---|---|---|
| React Context | Low | Poor (All consumers re-render when any part of context value changes). | Easy |
| Redux (Toolkit) | High (Requires Actions, Reducers, Thunks, Providers, and Store setup). | Excellent (Selector-based subscription system). | Hard |
| Zustand | Extremely Low (A single file defines both store properties and actions). | Excellent (Selector-based subscription system). | Easy |
Key Advantages over Context:
Key Advantages over Redux:
Answer:
You define a store using the create function from the zustand package. This function accepts a callback that receives a set function (used to mutate state) and returns an object containing your state fields and actions.
Step 1: Defining the store
import { create } from 'zustand';
export const useStore = create((set) => ({
bears: 0,
increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
removeAllBears: () => set({ bears: 0 }),
}));
Step 2: Consuming inside a component
export function BearCounter() {
// Pass a selector function to pick the specific slice of state
const bears = useStore((state) => state.bears);
return <h1>{bears} around here ...</h1>;
}
export function Controls() {
const increasePopulation = useStore((state) => state.increasePopulation);
return <button onClick={increasePopulation}>one more bear</button>;
}
Answer:
When you trigger updates using the set function, Zustand automatically shallowly merges the returned object into the current state.
// Current state: { bears: 0, fishes: 5 }
set({ bears: 1 })
// Resulting state: { bears: 1, fishes: 5 }
You only need to supply the properties you wish to modify.
{ ...state, bears: state.bears + 1 }).set((state) => ({
nested: {
...state.nested,
childProp: 'new value'
}
}));
Answer: Yes! Zustand provides a clean separation between its vanilla core engine and its React hook wrapper. This is highly useful for writing business logic in plain utility files, inside Web Workers, or logging engines.
Every store instance exposes three vanilla functions:
getState(): Returns the current state snapshot synchronously.setState(nextState): Mutates the store state and notifies subscribers.subscribe(listener): Subscribes a callback to execute on any state changes.import { useStore } from './myStore';
// Retrieve values directly in utility JS files
const count = useStore.getState().bears;
// Mutate store outside components
useStore.setState({ bears: 100 });
// Watch changes programmatically
const unsubscribe = useStore.subscribe((state) => {
console.log("Bears count updated:", state.bears);
});
You've completed the 5 free sample questions. Get unrestricted lifetime access to every question, model answer, implementation challenge, and all 27+ technologies for a single payment.
₹399 India / $9 International · One-time settlement · Zero subscription