Skip to content

abhub23/React-Flip-Game

Repository files navigation

Zustand State and set Explained

1. Ways to Update State Using set

Zustand provides two ways to update state inside the store:

a) Functional Update (set((state) => {...}))

  • Use this when your update depends on the previous state.
  • It ensures that you always modify the latest state.
set((state) => ({ count: state.count + 1 }));

Why? The function receives state, so we can safely modify count based on the latest state.


b) Direct Object Update (set({...}))

  • Use this when updating state without depending on the previous state.
set({ user: newUser });

Why? We don’t need the previous state here, just replacing user.


2. Parentheses in set((state) => {...})

  • When returning an object in an arrow function, wrap it in () to avoid JavaScript confusion.

Correct:

set((state) => ({ count: state.count + 1 })); // ✅ Parentheses required

Incorrect (No return, JavaScript thinks {} is a function block)

set((state) => { count: state.count + 1 }); // ❌ Syntax error!

If using a function block {}, explicitly use return:

set((state) => {
  return { count: state.count + 1 };
});

3. When to Use Which?

Approach When to Use? Example
set((state) => ({ ... })) If updating based on previous state set((state) => ({ count: state.count + 1 }))
set({ ... }) If replacing state without needing the old state set({ user: newUser })

4. Why No Parentheses in Some Cases?

  • If an arrow function uses {}, it’s treated as a function block and requires an explicit return.
  • If it’s one line returning an object, wrap it in ().

With {} (Needs return)

set((theme) => {
  const toggle = theme.isDarkmode === "dark" ? "light" : "dark";
  localStorage.setItem("theme", toggle);
  return { isDarkmode: toggle }; // ✅ Explicit return needed
});

With () (Implicit Return)

set((state) => ({ count: state.count + 1 })); // ✅ No `return` needed

Summary

  • Use set((state) => ({ ... })) when updating based on previous state, and set({ ... }) when setting a new value directly.
  • Use () around objects when using an implicit return in arrow functions.
  • If using {}, you must use return.

🚀 Now you have a complete reference for Zustand state updates!