-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat<Compiler>: consider that the dispatch function from
useReducer
…
… is non-reactive (#29705) Summary The dispatch function from useReducer is stable, so it is also non-reactive. the related PR: #29665 the related comment: #29674 (comment) I am not sure if the location of the new test file is appropriate😅. How did you test this change? Added the specific test compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/useReducer-returned-dispatcher-is-non-reactive.expect.md.
- Loading branch information
Showing
10 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
28 changes: 28 additions & 0 deletions
28
...ompiler/src/__tests__/fixtures/compiler/error.modify-useReducer-state.expect.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useReducer } from "react"; | ||
|
||
function Foo() { | ||
let [state, setState] = useReducer({ foo: 1 }); | ||
state.foo = 1; | ||
return state; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
3 | function Foo() { | ||
4 | let [state, setState] = useReducer({ foo: 1 }); | ||
> 5 | state.foo = 1; | ||
| ^^^^^ InvalidReact: Mutating a value returned from 'useReducer()', which should not be mutated. Use the dispatch function to update instead (5:5) | ||
6 | return state; | ||
7 | } | ||
8 | | ||
``` | ||
7 changes: 7 additions & 0 deletions
7
...el-plugin-react-compiler/src/__tests__/fixtures/compiler/error.modify-useReducer-state.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useReducer } from "react"; | ||
|
||
function Foo() { | ||
let [state, setState] = useReducer({ foo: 1 }); | ||
state.foo = 1; | ||
return state; | ||
} |
57 changes: 57 additions & 0 deletions
57
...ts__/fixtures/compiler/useReducer-returned-dispatcher-is-non-reactive.expect.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import { useReducer } from "react"; | ||
|
||
function f() { | ||
const [state, dispatch] = useReducer(); | ||
|
||
const onClick = () => { | ||
dispatch(); | ||
}; | ||
|
||
return <div onClick={onClick} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: f, | ||
params: [], | ||
isComponent: true, | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useReducer } from "react"; | ||
|
||
function f() { | ||
const $ = _c(1); | ||
const [state, dispatch] = useReducer(); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
const onClick = () => { | ||
dispatch(); | ||
}; | ||
|
||
t0 = <div onClick={onClick} />; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: f, | ||
params: [], | ||
isComponent: true, | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div></div> |
17 changes: 17 additions & 0 deletions
17
...ompiler/src/__tests__/fixtures/compiler/useReducer-returned-dispatcher-is-non-reactive.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useReducer } from "react"; | ||
|
||
function f() { | ||
const [state, dispatch] = useReducer(); | ||
|
||
const onClick = () => { | ||
dispatch(); | ||
}; | ||
|
||
return <div onClick={onClick} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: f, | ||
params: [], | ||
isComponent: true, | ||
}; |