Skip to content

[Mistake]: The React compiler cannot memoize a callback function inside a loop, contrary to what the docs claim #8080

@DCzajkowski

Description

@DCzajkowski

Summary

The React Compiler docs claim that a callback inside a loop will be auto-memoized.

I was very surprised to learn this, because that callback captures the item, meaning every callback in that loop has a different context. This would most likely require a use of WeakMap to store each callback's reference for each item.

In the React Compiler Playground, it seems the callback is not memoized and no form of collection for callbacks is used: playground.react.dev demo

I've also conducted tests in this repo with live demo available.


I've also seen this example in your presentation @poteto at ReactConf, so I am tagging you here. Maybe you can point me in the right direction (am I missing something)?


P.S. In the same example, the docs also claim, that React.memo can be omitted. However, that does not seem to be the case in both the Compiler Playground and tests in my repo.

Page

https://react.dev/learn/react-compiler/introduction#before-react-compiler

Details

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions