Skip to main content
rule CX Codex

rerender-move-effect-to-event — Claude Code rule for useEffect

rerender-move-effect-to-event tells the agent to fire a side effect from the handler that caused it, not from useEffect watching a state flag.

MIT ★ 7,191 updated 2026-10-24

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/rerender-move-effect-to-event.md
Open on GitHub

What rerender-move-effect-to-event does

A click often becomes a state flag in React, and useEffect does the actual work. The rule treats that as a trap: the effect re-runs whenever any dependency moves, so an unrelated value can trigger a second submit.

The fix it asks for is smaller than the original code. A handleSubmit function posts to /api/register and raises the toast on the spot, inside the click. No submitted state, no dependency list to keep in sync.

Drop the file at .claude/rules/rerender-move-effect-to-event.md and Claude reads it as a project rule. The header marks impact as MEDIUM. On a codebase without React hooks there is nothing here to apply.

What it helps with

  • move a form submission out of useEffect and into the click handler
  • stop an effect from refiring when an unrelated dependency changes
  • spot click or drag actions modelled as state flags
  • explain to the agent why a user action should not become state plus effect

Example prompts for the agent

  • This form posts from a useEffect watching submitted — rewrite it so the request goes out in the click handler.
  • Scan src/ for effects that exist only to react to a button press.
  • Rewrite handleSubmit to call the API directly, with no intermediate state.

How to install rerender-move-effect-to-event

Manually: put the files in place

.claude/rules/rerender-move-effect-to-event.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about rerender-move-effect-to-event

How do I install rerender-move-effect-to-event in Claude Code?

Add the rule file at .claude/rules/rerender-move-effect-to-event.md inside your project.

What does rerender-move-effect-to-event do?

It tells the agent to run a submit, click or drag side effect inside the event handler instead of modelling the action as state plus useEffect.

How much impact does the rule claim?

The frontmatter labels it MEDIUM, aimed at avoiding extra effect runs and duplicated side effects.

Source text

rerender-move-effect-to-event.md · MIT
---
title: Put Interaction Logic in Event Handlers
impact: MEDIUM
impactDescription: avoids effect re-runs and duplicate side effects
tags: rerender, useEffect, events, side-effects, dependencies
---

## Put Interaction Logic in Event Handlers

If a side effect is triggered by a specific user action (submit, click, drag), run it in that event handler. Do not model the action as state + effect; it makes effects re-run on unrelated changes and can duplicate the action.

**Incorrect (event modeled as state + effect):**

```tsx
function Form() {
  const [submitted, setSubmitted] = useState(false)
  const theme = useContext(ThemeContext)

  useEffect(() => {
    if (submitted) {
      post('/api/register')
      showToast('Registered', theme)
    }
  }, [submitted, theme])

  return <button onClick={() => setSubmitted(true)}>Submit</button>
}
```

**Correct (do it in the handler):**

```tsx
function Form() {
  const theme = useContext(ThemeContext)

  function handleSubmit() {
    post('/api/register')
    showToast('Registered', theme)
  }

  return <button onClick={handleSubmit}>Submit</button>
}
```

Reference: [Should this code move to an event handler?](https://react.dev/learn/removing-effect-dependencies#should-this-code-move-to-an-event-handler)

© tech-leads-club · MIT · tech-leads-club/agent-skills

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set130

The whole set →
client-swr-dedup The client-swr-dedup rule recommends SWR to share requests between component instances and reuse cached data. It fits React projects that fetch data on the client. CX Codex +1 ★ 7,191repo stars rerender-memo-with-default-value rerender-memo-with-default-value — a rule that prevents memoization from breaking when React components have default values (functions, objects, arrays) in parameters. It instructs you to extract these defaults into module-level constants. CX Codex +1 ★ 7,191repo stars rendering-animate-svg-wrapper rendering-animate-svg-wrapper enforces wrapping svg elements in an outer div container for CSS animations. Enables GPU hardware acceleration across browsers. CX Codex +1 ★ 7,191repo stars rerender-derived-state-no-effect The rerender-derived-state-no-effect rule keeps derived values out of React state and avoids effects that mirror props. Use it when reviewing components for extra renders and state drift. CX Codex +1 ★ 7,191repo stars async-dependencies async-dependencies instructs Claude Code to execute partially dependent async tasks concurrently using better-all or native promise chains rather than blocking Promise.all calls. CX Codex +1 ★ 7,191repo stars server-cache-lru The server-cache-lru rule explains when React.cache() is too narrow and an LRU cache should share data between requests. Use it for sequential actions that reuse the same records. CX Codex +1 ★ 7,191repo stars advanced-event-handler-refs advanced-event-handler-refs guides Claude Code to keep event callbacks in refs or useEffectEvent, stopping React effects from re-subscribing on every render. CX Codex +1 ★ 7,191repo stars server-parallel-fetching The server-parallel-fetching rule shows how to compose React Server Components so independent data requests can run in parallel. Use it when one component waits on another component’s fetch. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open