When designing and implementing web interfaces to ensure they are usable by everyone.
中文简介正在准备中。当前显示原文说明。
When deciding where and how to store data in a modern web application.
中文简介正在准备中。当前显示原文说明。
查看安装方法安装前可以查看交给 AI 代理的指令内容。
Applications suffer from bugs and performance issues when state lives in the wrong place. This skill guides teams to categorize state by its nature (UI, server, URL, global) and store it appropriately, eliminating duplication, excessive re-renders, and synchronization bugs.
useState onlyconst [val, setVal] = useState(props.val) (causes sync bugs)useStateuseState❌ Anti-pattern (Wrong state locations, duplication, mirroring):
// Redux store has everything including UI state
const store = {
filters: { search: '', sort: 'name' }, // WRONG: belongs in URL
users: [...], // OK: server data
isModalOpen: false, // WRONG: UI-only state
theme: 'dark' // OK: truly global
};
// Component mirrors props and stores server data locally
const UserList = ({ searchQuery }) => {
const [search, setSearch] = useState(searchQuery); // ANTI-PATTERN: mirrors props
const [users, setUsers] = useState([]); // ANTI-PATTERN: manual server sync
useEffect(() => {
fetch(`/api/users?q=${search}`)
.then(res => res.json())
.then(data => setUsers(data)); // Manual sync to local state
}, [search]);
return <div>{users.map(u => <p>{u.name}</p>)}</div>;
};
✅ Correct pattern (Proper state separation):
const UserList = () => {
// 1. URL drives state (filters/search)
const [searchParams, setSearchParams] = useSearchParams();
const search = searchParams.get('q') || '';
// 2. Server data via React Query (cached, not Redux)
const { data: users } = useQuery({
queryKey: ['users', search],
queryFn: () => fetch(`/api/users?q=${search}`).then(r => r.json())
});
// 3. UI-only state stays local
const [isModalOpen, setIsModalOpen] = useState(false);
// 4. Global state from Context/Redux
const { theme } = useContext(ThemeContext);
return (
<div>
<input
value={search}
onChange={(e) => setSearchParams({ q: e.target.value })}
/>
<button onClick={() => setIsModalOpen(true)}>Add User</button>
{users?.map(u => <p>{u.name}</p>)}
</div>
);
};
还没有评价。欢迎分享你的使用感受。
无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。
还没有评论。来写第一条吧。
When designing and implementing web interfaces to ensure they are usable by everyone.
中文简介正在准备中。当前显示原文说明。
When creating or extending an HTTP API for client consumption.
中文简介正在准备中。当前显示原文说明。
When improving read performance and reducing database load.
中文简介正在准备中。当前显示原文说明。
When configuring automated build, test, and deployment workflows for a repository.
中文简介正在准备中。当前显示原文说明。
When asynchronously reviewing peer code before merging into the main branch.
中文简介正在准备中。当前显示原文说明。
When addressing slow application endpoints, high database CPU usage, or standardizing data access patterns.
中文简介正在准备中。当前显示原文说明。