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
.claude/rules/rerender-move-effect-to-event.mdWhat 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
useEffectand 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
useEffectwatchingsubmitted— 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
handleSubmitto 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.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/rerender-move-effect-to-event.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/rerender-move-effect-to-event.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/rerender-move-effect-to-event.mdManually: put the files in place
.clinerules/rerender-move-effect-to-event.mdStep 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