Repository navigation
Removing the dependency array in useEffect #29
Replies: 3 comments
|
Great question! You are right that the compiler's analysis gives us a solid foundation to automatically figure out dependencies. What's hard about this is not the compiler implementation, but more so the overall design of effects. We're looking at it holistically across both the runtime and compiler. It's not baked enough for us to share concrete details at this point in time, but this is something we're well aware of and have been doing a lot of active research on. Several of us are working on the useEffect space currently. I hope we can share more soon! |
|
Curious about the status of this @poteto, especially given this blog post from earlier this year:
The post makes it seem like the compiler can already do this, but as far as I can tell, it doesn't do this and the blog post is misleading. |
|
Considering how dependencies for With Here's what I'd suggest: Introduce a hook called something like const useCompilerEffect = (f: EffectCallback) => {
throw new Error(
'This hook can only be used within components / hooks that are optimized by the React Compiler.'
);
};Then, when you use it like this: useCompilerEffect(() => {
doSomethingWithFoo(foo);
});It will throw if the React compiler isn't enabled for that component / hook. If the Compiler optimizes it, it transforms the call into: useEffect(() => {
doSomethingWithFoo(foo);
}, [doSomethingWithFoo, foo]);This would be quite a minimal change and allows for incremental adoption. The main problem with this is approach is what happens when the compiler silently skips a component. I'd suggest making this a build error instead of silently skipping it, forcing developers to either resolve the issues that lead to skipping the component or to explicitly opt out of optimization by putting a We had to manually implement a React Compiler logger in our project that fails the build whenever a component is silently skipped. Otherwise, developers might skip manual memoization, assuming the component / hook is already being optimized by the Compiler when it actually isn't. I think this should be the default behavior: If a component is expected to be optimized but the compiler silently skips it, developers should be made aware through a build failure rather than having it happen silently. |
Uh oh!
There was an error while loading. Please reload this page.
What are the plans for using the React Compiler to remove the need for having a dependency array in
useEffect?It feels like it would be pretty easy to have the compiler automatically inject all the right dependencies, and surely this has been discussed internally. Although I guess there are backwards compatibility issues with the fact that not having a dependency array currently means running the effect after each render.
All reactions