Conversation
This validation ensures that React compiler-enabled apps remain correct. That is, code that errors with this validation is most likely ***invalid*** with React compiler is enabled (specifically, hook calls will be compiled to if-else memo blocks).
Hook guards are used extensively for Meta's react compiler rollouts. There, they're enabled for developers (for dev builds) and on e2e test runs. Let's enable by default for oss as well
### Examples of inputs this rule throws on
* Components should not be invoked directly as React Compiler could memoize the call to AnotherComponent, which introduces conditional hook calls in its compiled output.
```js
function Invalid1(props) {
const myJsx = AnotherComponent(props);
return <div> { myJsx } </div>;
}
```
* Hooks must be named as hooks. Similarly, hook calls may not appear in functions that are not components or hooks.
```js
const renamedHook = useState;
function Invalid2() {
const [state, setState] = renamedHook(0);
}
function Invalid3() {
const myFunc = () => useContext(...);
myFunc();
}
```
* Hooks must be directly called (from the body of a component or hook)
```
function call(fn) {
return fn();
}
function Invalid4() {
const result = call(useMyHook);
}
```
### Example of hook guard error (in dev build)
<img width="1237" alt="image" src="https://github.com/user-attachments/assets/e9ada403-b0d7-4840-b6d5-ad600519c6e6" />
Greptile OverviewGreptile Summaryenabled hook guards in dev mode by default to validate that React compiler-enabled apps follow hook rules correctly
Confidence Score: 0/5
Important Files ChangedFile Analysis
|
| 'React Compiler internal invariant violation: unexpected null dispatcher', | ||
| ); | ||
| } else { | ||
| return (originalDispatcher as any).useMemoCache(...args); |
There was a problem hiding this comment.
logic: hardcoded useMemoCache call will fail for other dispatcher methods like readContext, use, getCacheForType, etc. - should call (originalDispatcher as any)[key](...args) instead
Suggested change
| return (originalDispatcher as any).useMemoCache(...args); | |
| return (originalDispatcher as any)[key](...args); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/react-compiler-runtime/src/index.ts
Line: 93:93
Comment:
**logic:** hardcoded `useMemoCache` call will fail for other dispatcher methods like `readContext`, `use`, `getCacheForType`, etc. - should call `(originalDispatcher as any)[key](...args)` instead
```suggestion
return (originalDispatcher as any)[key](...args);
```
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mirror of facebook/react#32524
Original author: mofeiZ
This validation ensures that React compiler-enabled apps remain correct. That is, code that errors with this validation is most likely invalid with React compiler is enabled (specifically, hook calls will be compiled to if-else memo blocks).
Hook guards are used extensively for Meta's react compiler rollouts. There, they're enabled for developers (for dev builds) and on e2e test runs. Let's enable by default for oss as well
Examples of inputs this rule throws on
Components should not be invoked directly as React Compiler could memoize the call to AnotherComponent, which introduces conditional hook calls in its compiled output.
Hooks must be named as hooks. Similarly, hook calls may not appear in functions that are not components or hooks.
Hooks must be directly called (from the body of a component or hook)
Example of hook guard error (in dev build)