A function is called when values it doesn't depend on are changed #70
Replies: 2 comments 1 reply
-
|
Hey @garipov, yeah I see what you're saying about that compiled code. The React Compiler (Forget) is basically lumping together the checks for all props used in the component, so even if expensiveProcessing only cares about data, it re-runs when onClick changes too. It's kinda a trade-off in the current setup to keep things from getting too split up in deps, but it does cause extra work like you pointed out. function ExpensiveComponent({ data, onClick }) {
const processedData = useMemo(() => expensiveProcessing(data), [data]);
const handleClick = (item) => {
onClick(item.id);
};
// ...
}That way it skips recomputing unless data actually updates, and ignores onClick. Compiler won't mess with it if you do this manually. If you run into this a lot, maybe open a feature request on the docs for more precise auto-memo stuff? Let me know if that helps! |
Beta Was this translation helpful? Give feedback.
-
|
Hey @garipov, no worries, following the docs is exactly the point, and the compiler's still evolving, so this feedback is gold! That map twist is spot on; it creates a fresh array every render, nuking child memos downstream. If you've got time, drop a repro on the react-compiler GitHub (sandbox with/without the map), it'll give the team a nudge toward finer dep caching. Keep those sharp eyes peeled, man. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
While researching the example from the React website
I found that
expensiveProcessingfunction is called whenonClickhandler is changed, even though it doesn't depend ononClick. Here is compiled code:I believe this is not optimal, and the expected compiled code should look like:
Beta Was this translation helpful? Give feedback.
All reactions