DEV Community

Jeet Singh
Jeet Singh

Posted on

This was a feature not a bug🐞

A single ESLint error took me on an unexpected journey into the heart of the React Compiler this week. It was a powerful reminder that there's a deep story behind every rule. Here’s what I learned. 👇

The Problem
It started with a simple pattern: using an async function inside a useEffect hook. A lint rule flagged my setState call, which I thought was a mistake. Since the state update happened after an await, I was convinced it had to be a false positive.

The Investigation
My curiosity led me down a rabbit hole. I started by opening an issue on GitHub, but I wanted to understand the root cause. I decided to dive into the React repository myself and trace the logic.
The journey took me from the eslint-plugin-react-compiler wrapper, deep into the babel-plugin-react-compiler, and eventually to the exact file responsible for the logic: src/Validation/ValidateNoSetStateInEffects.ts.

The "Aha!" Moment 💡
After analyzing the compiler's validation code, I had my 'aha!' moment. The rule isn't a bug; it's a crucial feature designed to prevent memory leaks.
It correctly flags any setState call in the main body of an effect to protect against a classic race condition: the component could unmount while waiting for the await to finish, leading to an attempt to update a non-existent component.

The Takeaway
My biggest takeaway wasn't just about compilers, but about appreciating the "why" behind the Rules of Hooks. What seems like a simple linting error is often a carefully designed safeguard built on years of experience by the React team.
It was a humbling and incredibly valuable deep dive!
hashtag#ReactJS hashtag#WebDevelopment hashtag#OpenSource hashtag#JavaScript hashtag#LearnInPublic

Top comments (0)