|
| 1 | + |
| 2 | +## Input |
| 3 | + |
| 4 | +```javascript |
| 5 | +function Component() { |
| 6 | + let local; |
| 7 | + |
| 8 | + const reassignLocal = newValue => { |
| 9 | + local = newValue; |
| 10 | + }; |
| 11 | + |
| 12 | + const onClick = newValue => { |
| 13 | + reassignLocal('hello'); |
| 14 | + |
| 15 | + if (local === newValue) { |
| 16 | + // Without React Compiler, `reassignLocal` is freshly created |
| 17 | + // on each render, capturing a binding to the latest `local`, |
| 18 | + // such that invoking reassignLocal will reassign the same |
| 19 | + // binding that we are observing in the if condition, and |
| 20 | + // we reach this branch |
| 21 | + console.log('`local` was updated!'); |
| 22 | + } else { |
| 23 | + // With React Compiler enabled, `reassignLocal` is only created |
| 24 | + // once, capturing a binding to `local` in that render pass. |
| 25 | + // Therefore, calling `reassignLocal` will reassign the wrong |
| 26 | + // version of `local`, and not update the binding we are checking |
| 27 | + // in the if condition. |
| 28 | + // |
| 29 | + // To protect against this, we disallow reassigning locals from |
| 30 | + // functions that escape |
| 31 | + throw new Error('`local` not updated!'); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + return <button onClick={onClick}>Submit</button>; |
| 36 | +} |
| 37 | + |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +## Error |
| 42 | + |
| 43 | +``` |
| 44 | + 29 | }; |
| 45 | + 30 | |
| 46 | +> 31 | return <button onClick={onClick}>Submit</button>; |
| 47 | + | ^^^^^^^ InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (31:31) |
| 48 | +
|
| 49 | +InvalidReact: The function modifies a local variable here (5:5) |
| 50 | + 32 | } |
| 51 | + 33 | |
| 52 | +
|
| 53 | +ReactCompilerError: InvalidReact: This argument is a function which may reassign or mutate local variables after render, which can cause inconsistent behavior on subsequent renders. Consider using state instead (31:31) |
| 54 | +
|
| 55 | +InvalidReact: The function modifies a local variable here (5:5) |
| 56 | + at validateNoFreezingKnownMutableFunctions (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:113829:18) |
| 57 | + at runWithEnvironment (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:114076:7) |
| 58 | + at run (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:113959:10) |
| 59 | + at compileFn (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:114301:10) |
| 60 | + at tryCompileFunction (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:114793:19) |
| 61 | + at processFn (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:114730:25) |
| 62 | + at compileProgram (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:114638:22) |
| 63 | + at PluginPass.enter (/Users/joesavona/github/react/compiler/packages/babel-plugin-react-compiler/dist/index.js:115773:26) |
| 64 | + at newFn (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/visitors.js:172:14) |
| 65 | + at NodePath._call (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/path/context.js:49:20) |
| 66 | + at NodePath.call (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/path/context.js:39:18) |
| 67 | + at NodePath.visit (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/path/context.js:88:31) |
| 68 | + at TraversalContext.visitQueue (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/context.js:90:16) |
| 69 | + at TraversalContext.visitSingle (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/context.js:66:19) |
| 70 | + at TraversalContext.visit (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/context.js:113:19) |
| 71 | + at traverseNode (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/traverse-node.js:22:17) |
| 72 | + at traverse (/Users/joesavona/github/react/compiler/node_modules/@babel/traverse/lib/index.js:53:34) |
| 73 | + at transformFile (/Users/joesavona/github/react/compiler/node_modules/@babel/core/lib/transformation/index.js:80:31) |
| 74 | + at transformFile.next (<anonymous>) |
| 75 | + at run (/Users/joesavona/github/react/compiler/node_modules/@babel/core/lib/transformation/index.js:25:12) |
| 76 | + at run.next (<anonymous>) |
| 77 | + at /Users/joesavona/github/react/compiler/node_modules/@babel/core/lib/transform-ast.js:23:33 |
| 78 | + at Generator.next (<anonymous>) |
| 79 | + at evaluateSync (/Users/joesavona/github/react/compiler/node_modules/gensync/index.js:251:28) |
| 80 | + at sync (/Users/joesavona/github/react/compiler/node_modules/gensync/index.js:89:14) |
| 81 | + at stopHiding - secret - don't use this - v1 (/Users/joesavona/github/react/compiler/node_modules/@babel/core/lib/errors/rewrite-stack-trace.js:47:12) |
| 82 | + at transformFromAstSync (/Users/joesavona/github/react/compiler/node_modules/@babel/core/lib/transform-ast.js:43:83) |
| 83 | + at /Users/joesavona/github/react/compiler/packages/snap/dist/compiler.js:204:62 |
| 84 | + at Generator.next (<anonymous>) |
| 85 | + at /Users/joesavona/github/react/compiler/packages/snap/dist/compiler.js:37:71 |
| 86 | + at new Promise (<anonymous>) |
| 87 | + at __awaiter (/Users/joesavona/github/react/compiler/packages/snap/dist/compiler.js:33:12) |
| 88 | + at transformFixtureInput (/Users/joesavona/github/react/compiler/packages/snap/dist/compiler.js:186:12) |
| 89 | + at /Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:97:71 |
| 90 | + at Generator.next (<anonymous>) |
| 91 | + at /Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:14:71 |
| 92 | + at new Promise (<anonymous>) |
| 93 | + at __awaiter (/Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:10:12) |
| 94 | + at compile (/Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:44:12) |
| 95 | + at Object.<anonymous> (/Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:163:48) |
| 96 | + at Generator.next (<anonymous>) |
| 97 | + at /Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:14:71 |
| 98 | + at new Promise (<anonymous>) |
| 99 | + at __awaiter (/Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:10:12) |
| 100 | + at Object.transformFixture (/Users/joesavona/github/react/compiler/packages/snap/dist/runner-worker.js:148:12) |
| 101 | + at execFunction (/Users/joesavona/github/react/compiler/node_modules/jest-worker/build/workers/threadChild.js:148:17) |
| 102 | + at execHelper (/Users/joesavona/github/react/compiler/node_modules/jest-worker/build/workers/threadChild.js:127:5) |
| 103 | + at execMethod (/Users/joesavona/github/react/compiler/node_modules/jest-worker/build/workers/threadChild.js:131:5) |
| 104 | + at MessagePort.messageListener (/Users/joesavona/github/react/compiler/node_modules/jest-worker/build/workers/threadChild.js:49:7) |
| 105 | + at [nodejs.internal.kHybridDispatch] (node:internal/event_target:827:20) |
| 106 | +``` |
| 107 | + |
| 108 | + |
0 commit comments