home / skills / forge-town / skills / use-store-not-props-best-practice
This skill analyzes components and rewrites them to fetch data directly from the store, reducing prop usage and enhancing maintainability.
npx playbooks add skill forge-town/skills --skill use-store-not-props-best-practiceReview the files below or copy the command above to add this skill to your agents.
---
name: use-store-not-props-best-practice
description: 查看给出的组件并进行修改,尽可能的不要使用props,尽可能直接从store中获取数据。
---
# 使用 Store 而非 Props 最佳实践
此最佳实践的核心原则是:
1. **查看给出的组件并进行修改**:分析现有组件的代码结构
2. **尽可能的不要使用props**:减少或避免通过 props 传递数据
3. **尽可能直接从store中获取数据**:优先使用全局状态管理库直接访问数据
## 工作原理
- 检查组件是否通过 props 接收数据
- 确认所需数据是否在全局 store 中可用
- 重构组件以直接从 store 获取数据,移除不必要的 props
## 使用方法
在重构组件时应用此原则:优先检查 store 数据可用性,然后修改组件代码以直接访问 store 而非依赖 props 传递。
This skill inspects components and refactors them to prefer store-based data access instead of props. It analyzes component code, identifies props that can be replaced by store reads, and produces modified TypeScript components. The goal is to simplify component APIs and centralize state access for maintainability.
The skill scans the provided component source to detect props declarations and usages. It checks the global store shape (Redux, Zustand, Pinia, etc.) or a supplied state module to see if the same data exists there. When possible, it rewrites the component to import and read from the store directly and removes redundant props and prop typings. The output includes the updated component code and a brief changelog of removed props and new store hooks/imports.
Will this break components that rely on injected props for testability?
You should update tests to mock the store or provide a test store. If a prop is only used for testing, consider keeping it or creating a configurable store provider for tests.
Which stores are supported?
The skill targets common patterns (Redux selectors, Zustand hooks, Vuex/Pinia accessors) and generic module exports. If a custom store API is used, provide the access pattern or adapter.
How do you handle prop types and TypeScript interfaces?
The refactor updates component props interfaces to remove replaced fields and adds types for store-derived values. It preserves remaining prop typings and reports the changes.