Skip to content

Commit

Permalink
Use same example code for async effect warning (#15118)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Mar 15, 2019
1 parent ff4fb6d commit f0621fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
Expand Up @@ -4446,19 +4446,15 @@ const tests = {
errors: [
`Effect callbacks are synchronous to prevent race conditions. ` +
`Put the async function inside:\n\n` +
`useEffect(() => {\n` +
` let ignore = false;\n` +
` fetchSomething();\n` +
`\n` +
` async function fetchSomething() {\n` +
` const result = await ...\n` +
` if (!ignore) setState(result);\n` +
` }\n` +
`\n` +
` return () => { ignore = true; };\n` +
`}, ...);\n` +
`\n` +
`This lets you handle multiple requests without bugs.`,
'useEffect(() => {\n' +
' async function fetchData() {\n' +
' // You can await here\n' +
' const response = await MyAPI.getData(someId);\n' +
' // ...\n' +
' }\n' +
' fetchData();\n' +
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching',
],
},
{
Expand Down
22 changes: 9 additions & 13 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Expand Up @@ -112,19 +112,15 @@ export default {
message:
`Effect callbacks are synchronous to prevent race conditions. ` +
`Put the async function inside:\n\n` +
`useEffect(() => {\n` +
` let ignore = false;\n` +
` fetchSomething();\n` +
`\n` +
` async function fetchSomething() {\n` +
` const result = await ...\n` +
` if (!ignore) setState(result);\n` +
` }\n` +
`\n` +
` return () => { ignore = true; };\n` +
`}, ...);\n` +
`\n` +
`This lets you handle multiple requests without bugs.`,
'useEffect(() => {\n' +
' async function fetchData() {\n' +
' // You can await here\n' +
' const response = await MyAPI.getData(someId);\n' +
' // ...\n' +
' }\n' +
' fetchData();\n' +
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching',
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.js
Expand Up @@ -358,7 +358,7 @@ function commitHookEffectList(
' // ...\n' +
' }\n' +
' fetchData();\n' +
'}, [someId]);\n\n' +
`}, [someId]); // Or [] if effect doesn't need props or state\n\n` +
'Learn more about data fetching with Hooks: https://fb.me/react-hooks-data-fetching';
} else {
addendum = ' You returned: ' + destroy;
Expand Down

0 comments on commit f0621fe

Please sign in to comment.