Master state management in 8 core principles
State management is the backbone of modern apps. Get it wrong, and you'll spend months debugging "ghost bugs." Get it right, and your app becomes predictable, scalable, and maintainable.
This repository breaks down state management mastery into 8 essential principles that will transform how you build applications.
Get it wrong, and you'll spend months debugging "ghost bugs" where data appears and disappears mysteriously. Get it right, and your app becomes predictable, scalable, and maintainable.
Every piece of data should have ONE authoritative source. When you duplicate state across components, you're asking for synchronization nightmares. One source, multiple subscribers.
Map out how data moves through your app before writing code:
- Where does data originate?
- Who needs to read it?
- Who can modify it?
- When should updates trigger re-renders?
Instead of one massive global state object, break it into focused, independent chunks. User profile, shopping cart, and app settings shouldn't live in the same state bucket. Fine-grained reactivity = better performance.
- Local state: Component-specific data (form inputs, toggles)
- Shared state: Cross-component data (user auth, theme)
- Server state: API responses, cache management
Each has different patterns and tools.
π’ Simple apps (Local state, few components):
- React:
useState,useReducer - Vue:
ref(),reactive()
π‘ Medium complexity (Shared state, multiple features):
- React: Stunk, Zustand, Context API, Effector
- Vue: Pinia, Effector
π΄ Complex apps (Enterprise scale, heavy async):
- React: Stunk, Zustand, RTK, Jotai, Effector
- Vue: Pinia + plugins, Effector
π Framework-agnostic solutions:
- Stunk: Fine-grained reactivity, works with all frameworks
- Effector: Works with any UI or server framework. Tested with React, Solid, Vue. (https://github.com/effector/effector)
π‘ Pro tip: Start simple and migrate up as complexity grows. Don't over-engineer from day one.
Good state management should let you:
- See all state changes over time
- Replay actions to reproduce bugs
- Rollback to previous states
- Track which actions caused which updates
π‘Pro Tip: Stunk has inbuilt Time-Travel (withHistory())
When you follow these principles, your apps will have:
- Predictable updates
- Easier debugging
- Better performance
- Simpler testing
- Happier developers
import { chunk } from "stunk";
const count = chunk(0);
// Get current value
console.log(count.get()); // 0
// Update value
count.set(5);
count.set((prev) => prev + 1); // 6
// Subscribe to changes
count.subscribe((value) => {
console.log('Count changed:', value);
});
// Reset to initial value
count.reset(); // Back to 0import { chunk, computed } from "stunk";
const price = chunk(100);
const quantity = chunk(2);
// Auto-updates when price or quantity changes
const total = computed(
[price, quantity],
(priceValue, quantityValue) => priceValue * quantityValue
);
total.subscribe((value) => {
console.log('Total:', value); // 200
});
quantity.set(3); // Triggers: "Total: 300"import { chunk, select } from "stunk";
// β Bad: Duplicated state
let userProfile = { name: 'John', email: '[email protected]' };
let userName = 'John';
let userEmail = '[email protected]';
// Synchronization nightmare! Update one, forget the others
// β
Good: Single source of truth
const userState = chunk({
name: 'John',
email: '[email protected]',
avatar: '/avatar.jpg'
});
const userName = select(userState, (state) => state.name);
const userEmail = select(userState, (state) => state.email);
// Subscribe to changes
userName.subscribe((name) => {
console.log('Name updated:', name);
});
userEmail.subscribe((email) => {
console.log('Email updated:', email);
});
// Update once, all derived values automatically update
userState.set({
name: 'Jane',
email: '[email protected]',
avatar: '/new-avatar.jpg'
});- Stunk - Framework-agnostic, fine-grained state management that embodies these principles
- Zustand - Lightweight React state management
- Pinia - Vue's intuitive state management
- Effector - Meet the new standard for modern TypeScript development. Type safe, reactive, framework agnostic.
Found these principles helpful? Contributions welcome:
- Share real-world examples
- Add framework integrations
- Improve documentation
- Report issues
MIT License - see LICENSE for details.
β Star this repo if these principles helped you master state management!
Building better applications through better state management.