Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)13k · #11389

no-use-effect

Skill này làm gì

Skill dạy agent AI bỏ hook useEffect của React và dùng các lựa chọn an toàn hơn. Skill dành cho lập trình viên React muốn component sạch hơn và dễ gỡ lỗi hơn.

Khi nào nên dùng

  • ✓Khi agent sắp viết một component React mới.
  • ✓Khi bạn đang xem lại code dùng useEffect và muốn đơn giản hóa nó.
  • ✓Khi agent thêm useEffect để đề phòng và bạn muốn bỏ nó đi.

Liên kết skill

https://github.com/humanlayer/humanlayer/blob/main/.claude/skills/no-use-effect/SKILL.md

Sao 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

Hãy dùng skill này để giúp tôi: https://github.com/humanlayer/humanlayer/blob/main/.claude/skills/no-use-effect/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Refactor component React của tôi để không dùng useEffect, và giải thích từng chỗ thay thế.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent tìm hiểu mỗi effect đang làm gì, rồi chọn một trong năm cách thay thế, chẳng hạn tính giá trị ngay trong dòng hoặc dùng event handler. Có một tùy chọn chạy một lần khi mount cho các trường hợp hiếm.

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. Use derived state, event handlers, data-fetching libraries, or useMountEffect instead.

Quick Reference

This skill is dervied from the React docs: You Might Not Need an Effect. IMPORTANT: when this skill is invoked you MUST read this post in entirety.

Instead of useEffect for... Use
Deriving state from other state/props Inline computation (Rule 1)
Fetching data useQuery / data-fetching library (Rule 2)
Responding to user actions Event handlers (Rule 3)
One-time external sync on mount useMountEffect (Rule 4)
Resetting state when a prop changes key prop on parent (Rule 5)

When to Use This Skill

  • Writing new React components
  • Refactoring existing useEffect calls
  • Reviewing PRs that introduce useEffect
  • An agent adds useEffect "just in case"

Workflow

1. Identify the useEffect

Determine what the effect is doing -- deriving state, fetching data, responding to an event, syncing with an external system, or resetting state.

2. Apply the Correct Replacement Pattern

Use the five rules below to pick the right replacement.

3. Verify

bun --bun run typecheck -- --filter=<package>
bun --bun run test -- --filter=<package>

The Escape Hatch: useMountEffect

For the rare case where you need to sync with an external system on mount:

The implementation wraps useEffect with an empty dependency array to make intent explicit:

export function useMountEffect(effect: () => void | (() => void)) {
  /* eslint-disable no-restricted-syntax */
  useEffect(effect, []);
}

Replacement Patterns

Rule 1: Derive state, do not sync it

Most effects that set state from other state are unnecessary and add extra renders.

// BAD: Two render cycles - first stale, then filtered
function ProductList() {
  const [products, setProducts] = useState([]);
  const [filteredProducts, setFilteredProducts] = useState([]);

  useEffect(() => {
    setFilteredProducts(products.filter((p) => p.inStock));
  }, [products]);
}

// GOOD: Compute inline in one render
function ProductList() {
  const [products, setProducts] = useState([]);
  const filteredProducts = products.filter((p) => p.inStock);
}

Smell test: You are about to write useEffect(() => setX(deriveFromY(y)), [y]), or you have state that only mirrors other state or props.

Rule 2: Use data-fetching libraries

Effect-based fetching creates race conditions and duplicated caching logic.

// BAD: Race condition risk
function ProductPage({ productId }) {
  const [product, setProduct] = useState(null);

  useEffect(() => {
    fetchProduct(productId).then(setProduct);
  }, [productId]);
}

// GOOD: Query library handles cancellation/caching/staleness
function ProductPage({ productId }) {
  const { data: product } = useQuery(['product', productId], () =>
    fetchProduct(productId)
  );
}

Smell test: Your effect does fetch(...) and then setState(...), or you are re-implementing caching, retries, cancellation, or stale handling.

Rule 3: Event handlers, not effects

If a user clicks a button, do the work in the handler.

// BAD: Effect as an action relay
function LikeButton() {
  const [liked, setLiked] = useState(false);

  useEffect(() => {
    if (liked) {
      postLike();
      setLiked(false);
    }
  }, [liked]);

  return <button onClick={() => setLiked(true)}>Like</button>;
}

// GOOD: Direct event-driven action
function LikeButton() {
  return <button onClick={() => postLike()}>Like</button>;
}

Smell test: State is used as a flag so an effect can do the real action, or you are building "set flag -> effect runs -> reset flag" mechanics.

Rule 4: useMountEffect for one-time external sync

Good uses: DOM integration (focus, scroll), third-party widget lifecycles, browser API subscriptions.

// BAD: Guard inside effect
function VideoPlayer({ isLoading }) {
  useEffect(() => {
    if (!isLoading) playVideo();
  }, [isLoading]);
}

// GOOD: Mount only when preconditions are met
function VideoPlayerWrapper({ isLoading }) {
  if (isLoading) return <LoadingScreen />;
  return <VideoPlayer />;
}

function VideoPlayer() {
  useMountEffect(() => playVideo());
}

Use useMountEffect for stable dependencies (singletons, refs, context values that never c

Câu hỏi thường gặp

Cái gì thay thế useEffect?

Tính toán ngay trong dòng, thư viện lấy dữ liệu, event handler, hook chạy một lần khi mount, và prop key để reset state.

Nó có cấm hẳn useEffect không?

Nó nói không bao giờ gọi trực tiếp useEffect và thay vào đó dùng các lựa chọn đã liệt kê.

Lời khuyên này đến từ đâu?

Nó dựa trên hướng dẫn trong tài liệu React về khi nào không cần effect.

Chi tiết

Mô tả gốc của tác giả

Enforce the no-useEffect rule when writing or reviewing React code. ACTIVATE when writing React components, refactoring existing useEffect calls, reviewing PRs with useEffect, or when an agent adds useEffect "just in case." Provides the five replacement patterns and the useMountEffect escape hatch.

repo: humanlayer/humanlayer

Độ phổ biến

Sao GitHub
13k
Vị trí xếp hạng
#11389
Giấy phép
Apache-2.0
Cập nhật
31/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự