A function is called when values it doesn't depend on are changed #70
Replies: 1 comment
-
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.
Uh oh!
There was an error while loading. Please reload this page.
-
While researching the example from the React website
I found that
expensiveProcessing
function is called whenonClick
handler 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