rerender-memo-with-default-value — Claude Code rule
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.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/rerender-memo-with-default-value.mdWhat rerender-memo-with-default-value does
When a memoized React component declares an optional parameter with a default non-primitive value—such as a function, object, or array—calling the component without that parameter creates a new instance on each render. Since memo() relies on strict equality comparison, these fresh instances fail the check, and memoization breaks.
The rule shows how to move the default value outside the component into a constant. Instead of { onClick = () => {} } inline, define const NOOP = () => {} at module scope and use { onClick = NOOP } in the parameter list. This keeps the value stable across renders.
Once installed in .claude/rules/rerender-memo-with-default-value.md, Claude Code applies the rule automatically when reviewing React code.
What it helps with
- Fix broken memoization in components with default function parameters
- Stop unnecessary rerenders caused by new object instances
- Extract unstable defaults into constants to restore memo efficiency
Example prompts for the agent
- Review my UserAvatar component where onClick defaults to () => {} and suggest a fix
- Find all memoized components in this file that have object or function defaults
- Refactor optional parameters in memo() components so memoization actually works
How to install rerender-memo-with-default-value
Manually: put the files in place
.claude/rules/rerender-memo-with-default-value.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-memo-with-default-value.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/rerender-memo-with-default-value.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/rerender-memo-with-default-value.mdManually: put the files in place
.clinerules/rerender-memo-with-default-value.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about rerender-memo-with-default-value
How do I install the rerender-memo-with-default-value rule in Claude Code?
Place the rule file at .claude/rules/rerender-memo-with-default-value.md. Claude Code automatically loads all rules from that directory when you open your project.
What does this rule do?
The rule detects when a memoized React component has a default value for a parameter (a function, object, or array) defined inline in the function signature. This breaks memoization because a new instance is created on every render. The rule recommends extracting that default into a constant.
Why does memo() not work when defaults are functions in the parameter list?
The memo() function compares props using strict equality (===). If a default is defined as () => {}, a fresh function is created on each render. Since they are different instances, memo() sees the props as changed and rerenders the component.
Source text
rerender-memo-with-default-value.md · MIT
---
title: Extract Default Non-primitive Parameter Value from Memoized Component to Constant
impact: MEDIUM
impactDescription: restores memoization by using a constant for default value
tags: rerender, memo, optimization
---
## Extract Default Non-primitive Parameter Value from Memoized Component to Constant
When memoized component has a default value for some non-primitive optional parameter, such as an array, function, or object, calling the component without that parameter results in broken memoization. This is because new value instances are created on every rerender, and they do not pass strict equality comparison in `memo()`.
To address this issue, extract the default value into a constant.
**Incorrect (`onClick` has different values on every rerender):**
```tsx
const UserAvatar = memo(function UserAvatar({ onClick = () => {} }: { onClick?: () => void }) {
// ...
})
// Used without optional onClick
<UserAvatar />
```
**Correct (stable default value):**
```tsx
const NOOP = () => {};
const UserAvatar = memo(function UserAvatar({ onClick = NOOP }: { onClick?: () => void }) {
// ...
})
// Used without optional onClick
<UserAvatar />
```
© tech-leads-club · MIT · tech-leads-club/agent-skills