// LOADING
// LOADING
// LOADING_ARTICLE
State management in React and Next.js isnβt about picking a single βbestβ approach β itβs about using the right tool for the right job. Poor state placement leads to performance issues, messy code, and unnecessary re-renders.
In this post, weβll break down:
useState)Local state belongs in a component when:
jsconst [email, setEmail] = useState("");
This should stay local because only this form needs it.
jsconst [isOpen, setIsOpen] = useState(false);
No need to store this globally unless multiple components control the same modal.
Rule of thumb: If lifting the state to a parent or global store doesnβt provide any benefit β keep it local.
Use global state when:
jsconst user = useStore(state => state.user);
Used in Navbar, Dashboard, Profile, and API calls β perfect for global state.
jsconst theme = useStore(state => state.theme);
Accessed across the entire app.
jsconst cartItems = useStore(state => state.cart);
Used in Navbar, Checkout, and Product pages.
Rule of thumb: If three or more components need the same data β make it global.
β Bad:
jsconst App = () => ( <AppContext.Provider value={{ count, setCount }}>
This makes every consumer re-render when count changes.
β Better: Use Zustand or Redux for high-frequency updates.
jsconst MemoizedComponent = React.memo(Component);
Prevents re-rendering when props havenβt changed.
β Bad:
jsconst [state, setState] = useState({ name: "", age: 0 });
β Better:
jsconst [name, setName] = useState(""); const [age, setAge] = useState(0);
This prevents re-renders when only one value changes.
β Bad:
js<Component config={{ theme: "dark" }} />
β Better:
jsconst config = useMemo(() => ({ theme: "dark" }), []); <Component config={config} />