To fix this, make sure you have the dependency array of useMemo and useCallback correct, and the good thing is the compiler actually tells you what's missing. So when I pass my hook inside React Compiler, it says that, hey, you have min and the max values missing, so you should probably add that. And this is honestly quite useful. This feels like a very small issue, but it's actually quite common, and it can happen a lot when you're working in teams. Where I have worked on a hook, I have written down the exact dependency array, which is used in the logic, but some other person comes in, updates the logic, but forgets to update the dependency array.
The fix for this is just to update the dependency array to have all the dependencies, or even better, just remove the manual memoization and let React Compiler do its magic. Now you might be wondering, how do I find such compilation issues? Well, the best way is to use the eslint plugin to see the analysis from the compiler in the editor itself. It'll show you where you are breaking the rules of React, what you should do to fix them, and it also guides you to the right documentation to understand what the actual fix would be for that particular scenario. You can also use the React Compiler Playground to see the analysis and issues and the output from the compiler directly on the web. So you can just bring in your code, you don't have to worry about installing dependencies or anything else, it is static analysis, so you just bring in your JavaScript code or any TypeScript code, and you can see the output, any errors or any issues. It's honestly a really great tool to actually learn how the compiler is performing and how it is working internally as well.
The third caveat I encountered was the external libraries that you use in your project will not be optimized automatically unless the library itself uses the compiler during build. Now this was a surprising finding. I thought my entire project would be optimized, including any external components that I might be using. But I noticed that a lot of my components were still re-rendering, and these components were not really written by me. They were coming from a third-party package I was using. In this case, it's coming from a package called RQI, which provides high-level components for common UI elements like accordion, menu, and so on. So these components were not memorized by the compiler and they were still re-rendering due to some unnecessary re-render. Now this is not ideal. I wish there was a better solution, but honestly the fix is that the compiler is run on the actual source code of the library and the new version is published using the compiler in the build process, and then you can install it and get the full benefit of the compiler.
Comments