Skip to content

Commit f0621fe

Browse files
authored
Use same example code for async effect warning (#15118)
1 parent ff4fb6d commit f0621fe

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,19 +4446,15 @@ const tests = {
44464446
errors: [
44474447
`Effect callbacks are synchronous to prevent race conditions. ` +
44484448
`Put the async function inside:\n\n` +
4449-
`useEffect(() => {\n` +
4450-
` let ignore = false;\n` +
4451-
` fetchSomething();\n` +
4452-
`\n` +
4453-
` async function fetchSomething() {\n` +
4454-
` const result = await ...\n` +
4455-
` if (!ignore) setState(result);\n` +
4456-
` }\n` +
4457-
`\n` +
4458-
` return () => { ignore = true; };\n` +
4459-
`}, ...);\n` +
4460-
`\n` +
4461-
`This lets you handle multiple requests without bugs.`,
4449+
'useEffect(() => {\n' +
4450+
' async function fetchData() {\n' +
4451+
' // You can await here\n' +
4452+
' const response = await MyAPI.getData(someId);\n' +
4453+
' // ...\n' +
4454+
' }\n' +
4455+
' fetchData();\n' +
4456+
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
4457+
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching',
44624458
],
44634459
},
44644460
{

packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,15 @@ export default {
112112
message:
113113
`Effect callbacks are synchronous to prevent race conditions. ` +
114114
`Put the async function inside:\n\n` +
115-
`useEffect(() => {\n` +
116-
` let ignore = false;\n` +
117-
` fetchSomething();\n` +
118-
`\n` +
119-
` async function fetchSomething() {\n` +
120-
` const result = await ...\n` +
121-
` if (!ignore) setState(result);\n` +
122-
` }\n` +
123-
`\n` +
124-
` return () => { ignore = true; };\n` +
125-
`}, ...);\n` +
126-
`\n` +
127-
`This lets you handle multiple requests without bugs.`,
115+
'useEffect(() => {\n' +
116+
' async function fetchData() {\n' +
117+
' // You can await here\n' +
118+
' const response = await MyAPI.getData(someId);\n' +
119+
' // ...\n' +
120+
' }\n' +
121+
' fetchData();\n' +
122+
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
123+
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching',
128124
});
129125
}
130126

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ function commitHookEffectList(
358358
' // ...\n' +
359359
' }\n' +
360360
' fetchData();\n' +
361-
'}, [someId]);\n\n' +
361+
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
362362
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching';
363363
} else {
364364
addendum = ' You returned: ' + destroy;

0 commit comments

Comments
 (0)