Skip to content

Conversation

@rickhanlonii
Copy link
Member

@rickhanlonii rickhanlonii commented Sep 3, 2025

Found by @gmoniava in #7734 (comment)

In the current version, passing an arrow function to the child breaks manual memoization, which means it's not an "equivalent" memoization like the text says.

This is actually a subtle bug that the compiler prevents - the compiler output for this does memoize correctly on onClick even with the function. That may be a good example to call out if we want to add to these docs.

This PR adds a note to call out the subtle bug, and explain how the Compiler works with or without the arrow function.
Screenshot 2025-09-03 at 10 44 50 AM

@github-actions
Copy link

github-actions bot commented Sep 3, 2025

Size changes

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌


Even though `handleClick` is wrapped in `useCallback`, the arrow function `() => handleClick(item)` creates a new function every time the component renders. This means that `Item` will always receive a new `onClick` prop, breaking memoization.

The React Compiler is able to optimize this correctly with or without the arrow function, ensuring that `Item` only re-renders when `props.onClick` changes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop the "the", just React Compiler

@rickhanlonii rickhanlonii merged commit b8e9faf into reactjs:main Sep 3, 2025
6 checks passed
@rickhanlonii rickhanlonii deleted the rh/compiler-intro branch September 3, 2025 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants