no-use-effect
Skill này làm gì
Hướng dẫn này giúp agent của bạn tránh một mẫu React phổ biến thường gây lỗi và render thừa. Dành cho lập trình viên viết hoặc rà soát các component và hook React.
Khi nào nên dùng
- ✓Khi agent của bạn tạo các component hoặc hook React mới.
- ✓Khi nó tái cấu trúc một lệnh useEffect hiện có.
- ✓Khi một pull request thêm useEffect mà bạn muốn rà soát.
Liên kết skill
https://github.com/fastrepl/anarlog/blob/main/.agents/skills/no-use-effect/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Rà soát component React này và bỏ mọi useEffect không thực sự cần thiết.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill trước tiên hỏi vì sao mỗi effect tồn tại, rồi thay nó bằng mẫu đơn giản nhất phù hợp. Các mẫu đó gồm tính giá trị ngay trong lúc render, dùng thư viện dữ liệu để lấy dữ liệu, xử lý hành động người dùng trong trình xử lý sự kiện, và một hook hẹp cho thiết lập một lần thật sự.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
No useEffect
Never call useEffect directly unless the work is a true mount-only sync with an external system and no simpler pattern fits. Prefer a render-time computation, a query or mutation hook, an event handler, or a remount boundary instead.
Workflow
- Classify why the effect exists.
- Replace it with the narrowest pattern below.
- Verify with the project's normal lint, typecheck, and tests.
Replacement Patterns
1. Derive state, do not sync it
Replace useEffect(() => setX(derive(y)), [y]) with inline computation or the project's existing memoization conventions.
const filteredProducts = products.filter((product) => product.inStock);
Smell test: state only mirrors props or other state.
2. Use query or mutation libraries for async work
Replace fetch-in-effect code with the app's data layer.
const productQuery = useQuery({
queryKey: ["product", productId],
queryFn: () => fetchProduct(productId),
});
Smell test: the effect fetches, awaits, retries, cancels, or writes async results into local state.
3. Use event handlers for user actions
Do the work where the action happens instead of setting a flag and reacting later.
const handleLike = () => {
postLike();
};
Smell test: state exists only to trigger an effect, then gets reset.
4. Use useMountEffect for mount-only external sync
Use this only for work that is naturally "set up on mount, clean up on unmount": DOM integration, subscriptions, third-party widgets, and stable singleton bindings.
If the project already has a useMountEffect, use it. If not, add a small wrapper:
import { useEffect } from "react";
export function useMountEffect(effect: () => void | (() => void)) {
useEffect(effect, []);
}
Prefer changing component structure so the component only mounts once preconditions are satisfied.
Smell test: the effect is a one-time setup or teardown for something outside React.
5. Use key to reset component state
If a component should behave like a fresh instance when an ID or entity changes, remount it from the parent.
return <VideoPlayer key={videoId} videoId={videoId} />;
Smell test: the effect exists only to reset local state when props change.
Review Standard
Treat new direct useEffect usage as a bug by default. Ask which of the five patterns applies. Accept direct useEffect only with explicit justification that the code is a mount-only external sync and that useMountEffect is the intended escape hatch.
Structure React Code This Way
Order component logic like this:
- Hooks that read external state or data.
- Local state.
- Derived values computed during render.
- Event handlers.
- Early returns.
- Render output.
Do not introduce mirrored state just to make an effect possible.
Câu hỏi thường gặp
Khi nào vẫn được dùng useEffect?
Chỉ để đồng bộ với một hệ thống bên ngoài chạy một lần khi mount, và chỉ khi không có mẫu đơn giản hơn phù hợp.
Skill có thay đổi cách ứng dụng của tôi hoạt động không?
Nó nhằm giữ hành vi như cũ, và hướng dẫn yêu cầu bạn chạy các kiểm tra lint, kiểu và test thông thường sau đó.
Giá trị dẫn xuất là gì?
Là giá trị được tính từ các giá trị khác ngay trong lúc render, thay vì được sao chép vào một biến state riêng.
Chi tiết
Mô tả gốc của tác giả
Avoid direct React `useEffect` usage when writing or reviewing React components and hooks. Use this skill when creating new React code, refactoring existing `useEffect` calls, reviewing PRs that introduce `useEffect`, or when an agent reaches for `useEffect` as a default. Replace effects with derived state, query or mutation libraries, event handlers, `useMountEffect` for mount-only external sync, or `key`-based remounts.
repo: fastrepl/anarlog
Độ phổ biến
- Sao GitHub
- 9.7k
- Vị trí xếp hạng
- #12213
- Giấy phép
- MIT
- Cập nhật
- 1/11/2026
Bình luận
- …
Thêm skill từ kho mã này
sqlite-schema-design
9.7kGiúp agent của bạn thiết kế hoặc rà soát bảng đồng bộ giữa các thiết bị bằng SQLite Sync, theo quy tắc về khóa, giá trị mặc định và thay đổi schema.
anarlog
9.7kGiúp agent của bạn tìm trong ghi chú cuộc họp Anarlog, bản tóm tắt, bản chép lời, người tham gia và việc cần làm, hoặc nhập một bản ghi âm cục bộ.
Skill tương tự
angular-developer
280kGiúp agent của bạn viết code Angular và tư vấn cấu trúc dự án, hữu ích khi bắt đầu dự án mới hoặc cải thiện hiệu suất.
frontend-a11y
280kGiúp agent của bạn làm cho giao diện web dễ tiếp cận với mọi người. Đảm bảo website và ứng dụng hoạt động tốt với trình đọc màn hình và điều hướng bằng bàn phím.
frontend-patterns
280kGiúp agent của bạn áp dụng các mẫu hiện đại trong phát triển frontend. Tối ưu hiệu năng, quản lý trạng thái và tuân theo thực hành tốt nhất với React và Next.js.