Comments (3)
So that means that React compiler does not auto memoize everything with the correct dependencies.
No. The compiler does a conservative analysis and uses the correct set of dependencies given the values that are being memoized. In this case, there are two values being memoized together, so we emit the union of their dependencies. That is the only correct thing to do.
There are alternative ways we could memoize — for example, entering, exiting, and re-entering memoization blocks — that have different tradeoffs. We may explore those later, but so far we've found that they have unacceptable tradeoffs such as high code size.
from react.
Thanks for posting. A simplified version of what's going on here is this:
const [data, refetch] = useData();
const y = deepClone(data); // y becomes mutable here
const t0 = <button onClick={refetch}> // this button gets memoized with `y`, so `refetch` becomes a dependency
const t1 = maybeMutate(y); // y is last mutated here
return <>{t0}{t1}</>;
Ie, the compiler currently sees JSON.stringify(y)
and thinks that it could be a mutation of y, and this affects the memoization. We have type declarations that understand the mutability semantics of various built-ins, but haven't added JSON.stringify yet (it's a fairly complex one to type because of the replacer callback). This is definitely on our radar to improve, but this is working as designed. Not memoizing in this way could cause us to miss mutations and produce incorrect results.
I'm going to close this since it's working as designed, but we will be working over time to type more builtins, including JSON.stringify.
from react.
Ok, probably the answer I was looking for is in:
this button gets memoized with
y
, sorefetch
becomes a dependency
So that means that React compiler does not auto memoize everything with the correct dependencies.
When you memoize y
manually, you'd do:
const y = useMemo(() => deepClone(data), [data]);
while what React compiler does is more equivalent to:
const y = useMemo(() => deepClone(refetch), [data, refetch]); // With refetch also invalidating the memo'd value
This perhaps should be noted in the docs somewhere that you may need to memo by hand in such cases that "precise dependencies" matter.
from react.
Related Issues (20)
- [React 19] When multiple components call `use` inside a `Suspense`, it crosses the boundary of `Suspense` and stops rendering of other `Suspense` as well HOT 2
- [Compiler]: Type inference information does not propagate past phi nodes HOT 1
- Bug: "Should not already be working" Error Provides No Information (Not Even the Name Indicates What's Wrong)
- [React 19] React-Dom bundle increase 200kb HOT 3
- Bug: Expected Static Flag Was Missing
- [React 19] useEffect does not re-fire on hot reload with React 19 and vite HOT 1
- Bug: Hundreds render of Suspense child with hydration error HOT 5
- Bug: Receiving `Error: Should have a queue. This is likely a bug in React. Please file an issue` on usage of `next-safe-action`'s `useAction` hook HOT 1
- [React 19] infinite loop when using React lazy & functional formAction together. HOT 4
- The const array variable value populated in a function does not reflect the updated values when called in another function in Firefox HOT 2
- Bug: DoS vulnerability in ws dependency
- [React 19] Inconsistent "cache" api with Async Operations in react-server HOT 2
- [Compiler Bug]: Increment expression in for-loop loses assignment operation HOT 4
- Bug: TypeError: Cannot read properties of null (reading 'useState')
- Ensures role attribute has an appropriate value for the element (aria-allowed-role - https://dequeuniversity.com/rules/axe/4.7/aria-allowed-role?application=msftAI) HOT 1
- Ensures ARIA attributes are allowed for an element's role (aria-allowed-attr - https://accessibilityinsights.io/info-examples/web/aria-allowed-attr)Bug: HOT 1
- Bug: HOT 1
- [VLG]¿Cómo hablar con Vueling por teléfono? @telefono españa HOT 1
- hook invalid
- Feature request: Expose additional profiling tools for library testing HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.