State management often feels like playing Jenga: a delicate balance between predictability and performance. When objects enter the equation, the tower wobbles. Modern frameworks like React, Vue, and Svelte struggle with a fundamental JavaScript reality: object comparison by reference, not value. Let's dissect why even seasoned developers trip over this and how to implement robust solutions.
The Mutation Mirage
Consider a React component tracking a user's preferences:
const UserPreferences = () => {
const [prefs, setPrefs] = useState({ darkMode: false, notifications: true });
const toggleDarkMode = () => {
prefs.darkMode = !prefs.darkMode; // Direct mutation
setPrefs(prefs); // Same object reference
};
return <ChildComponent config={prefs} />;
};