react-zustand-patterns
Apply Zustand state management patterns
Zustand's simplicity hides timing complexities that cause subtle bugs. Learn patterns for synchronous set() behavior, stale closure prevention, async actions, and selector optimization to write reliable React state management.
Скачать ZIP навыка
Загрузить в Claude
Перейдите в Settings → Capabilities → Skills → Upload skill
Включите и начните использовать
Протестировать
Использование «react-zustand-patterns». Explain why set() is synchronous but my component still shows old values after calling it
Ожидаемый результат:
- set() updates the Zustand store immediately and synchronously
- getState() will show the new value right after set()
- However, React components only re-render in the next batched update cycle
- The component function re-executes with old props/state until React processes the batch
- Use useStore.getState() directly if you need immediate access outside component renders
Использование «react-zustand-patterns». How do I properly handle async data fetching in my Zustand store
Ожидаемый результат:
- Use async keyword on the action function
- Set loading state to true before the fetch
- Read state after await using get() to ensure fresh values
- Handle errors with try/catch and set error state
- Return the promise so callers can await completion
Использование «react-zustand-patterns». Why is my React component re-rendering too often with Zustand
Ожидаемый результат:
- Check if your selector returns a new object reference each time
- Use shallow comparison with zustand/shallow or useShallow hook
- Split into multiple selectors that return primitive values
- Memoize derived values outside the selector if expensive
Аудит безопасности
БезопасноPure documentation skill containing educational content about Zustand state management patterns. All 52 static findings are false positives: markdown code block delimiters were misidentified as shell command execution, TypeScript syntax patterns as cryptographic algorithms, and feature documentation as executable code. No executable code, network calls, file system access, or credential handling exists.
Факторы риска
🌐 Доступ к сети (3)
⚙️ Внешние команды (35)
Оценка качества
Что вы можете построить
Fix State Timing Bugs
Debug why components see stale values after set() calls due to React's batched render behavior
Build Async Store Actions
Implement robust async data fetching with proper loading states, error handling, and race condition prevention
Optimize Render Performance
Prevent unnecessary re-renders by fixing selectors that return new objects on every render cycle
Попробуйте эти промпты
Explain how set() works synchronously but React re-renders are batched. Show why getState() returns new values immediately but components might see old values
How do I avoid stale closures in Zustand? Show why data captured in a callback becomes stale after await and how to use get() correctly
Write a complete async action pattern in Zustand with loading state, error handling, and race condition protection using get() checks
Fix this selector that causes unnecessary re-renders: const data = useStore((s) => ({ name: s.name, count: s.count }))Лучшие практики
- Always use get() or getState() after any await to ensure fresh state values
- Use shallow comparison or multiple selectors to prevent object creation in selectors
- Add devtools middleware during development to trace state changes
Избегать
- Relying on closure-captured values after async operations without using get()
- Returning new objects from selectors that cause unnecessary component re-renders
- Skipping the async keyword on store actions that return promises
Часто задаваемые вопросы
Is Zustand compatible with React Native?
What Zustand versions are supported?
How do I debug state changes in production?
Is my data safe with persist middleware?
Why do my selectors cause infinite re-renders?
How does Zustand compare to Redux?
Сведения для разработчиков
Автор
CJHarmathЛицензия
MIT
Репозиторий
https://github.com/CJHarmath/claude-agents-skills/tree/main/skills/react-zustand-patternsСсылка
main
Структура файлов
📄 SKILL.md