React developers, rejoice! The latest core feature by the React team,Ā React Forget, is here to revolutionize how we write performant React components. With React Forget, developers no longer need to manually useĀ useMemo
Ā andĀ useCallback
. Instead, this new feature automates memoization in React components, making it easier than ever to write optimized code. Sounds too good to be true, right? Well, itās not. Letās talk about it more.
So what exactly is React Forget? In short, itās anĀ auto-memoizing compilerĀ that optimizes reactivity at a component level. It ensures that your application re-renders only when the state values meaningfully change. From an implementation perspective, it means automatic memorization with the end goal being optimizing the performance of our React application.
In this blog post, weāll take a closer look at React Forget. Weāll explore the current state withĀ
useMemo
Ā andĀuseCallback
, the mechanics of React Forget, and the updates regarding this projectās release.
React Forget: What?
React Forget was unveiled during theĀ React Conf. 2021, and it promises to enhance component re-rendering by detecting the need forĀ useMemo
Ā orĀ useCallback
. The compiler injects code with similar behaviour to these structures, thereby optimizing the performance of your React application. Dan Abramov, a key member of the React team, suggests that React Forget might even eliminate the need forĀ React.memo()
.
React Forget essentially shifts the re-rendering behaviour of React from object identity changes to semantic value changes, without incurring the runtime cost of deep comparisons. The compiler apparently memoizes not only the calculation ofĀ useMemo()
Ā results but alsoĀ the resulting React element objects returned by the component.
React Forget: Why?
React developers have been usingĀ useMemo
Ā andĀ useCallback
Ā hooks to optimize their applications for a long time. These hooks are used to memoize the results of expensive calculations and functions, respectively. However, the problem withĀ useMemo
Ā andĀ useCallback
Ā is that they increase the mental overhead for developers. Developers have to manually use these hooks to optimize their applications. If the dependencies array isnāt correctly specified, it can lead to confusing bugs and performance issues. Using these Hooks also introduces some degree of hampered readability. In addition to this, these hooks have their own set of limitations. For eg.,Ā useMemo
Ā can only be used to memoize values that are calculated synchronously; it cannot be used to memoize values that are calculated asynchronously. And as forĀ useCallback
, it can only be used to memoize functions that are defined inline; it cannot be used to memoize functions that are defined outside the component.
Optimizing UX and DX
Developing an application that delivers a seamless User Experience (UX) often involves the use of memoization. However, this increases the mental overhead for developers, thereby creating a suboptimal Developer Experience(DX). React Forget aims to alleviate this issue by making the process of memoization automatic. Itās like having an automatic reactivity compiler that preemptively optimizes reactivity at a component level.
Tune into this discussion for more: āReact Without Memoā.
React Forget: Where and how?
React Forget is designed to be integrated into the build pipeline. Compilation occurs during the build phase, and the optimized code would be outputted in the bundle. To be crystal clear, React Forget wonāt execute during runtime. Once a stable version of React includes its required runtime API, developers will be able to install React Forget as a Babel plugin. This approach ensures that React Forget can seamlessly integrate with existing React projects. Under the hood, the compiler uses a custom code representation and transformation pipeline to perform low-level semantic analysis.
React Forget: When?
During the React Advanced conference, the developers of React Forget, Joe Savona and Mofei Zhang, provided insights into the current state and plans for the project in their talk āUnderstanding Idiomatic Reactā. Based on the milestones for this feature, React Forget is still in active development. While the project has made significant progress, it is not yet ready for release. It is specifically at the āinitial adoption + iterationā stage ā a limited rollout for beta testing.
Itās certainly going to be interesting to get a more hands-on experience with this feature. All eyes are on further reports from its trials to see how well it works out. It is anticipated to gain more traction with the target of being eventually open-sourced.
If this feature piqued your interest, you can also watch thisĀ tech talkĀ for more.