Make useEffect(async) warning more verbose (#14327)

* Make useEffect(async) warning more verbose

* Nit
This commit is contained in:
Dan Abramov
2018-11-27 13:05:10 +00:00
committed by GitHub
parent ee3ef3a079
commit f93f3402f7
2 changed files with 13 additions and 7 deletions
+11 -3
View File
@@ -336,9 +336,17 @@ function commitHookEffectList(
'useEffect function must return a cleanup function or ' +
'nothing.%s%s',
typeof destroy.then === 'function'
? ' Promises and useEffect(async () => ...) are not ' +
'supported, but you can call an async function inside an ' +
'effect.'
? '\n\nIt looks like you wrote useEffect(async () => ...) or returned a Promise. ' +
'Instead, you may write an async function separately ' +
'and then call it from inside the effect:\n\n' +
'async function fetchComment(commentId) {\n' +
' // You can await here\n' +
'}\n\n' +
'useEffect(() => {\n' +
' fetchComment(commentId);\n' +
'}, [commentId]);\n\n' +
'In the future, React will provide a more idiomatic solution for data fetching ' +
"that doesn't involve writing effects manually."
: '',
getStackByFiberInDevAndProd(finishedWork),
);
@@ -75,10 +75,8 @@ describe('ReactHooks', () => {
expect(() => {
root.update(<App return={Promise.resolve()} />);
}).toWarnDev([
'Warning: useEffect function must return a cleanup function or ' +
'nothing. Promises and useEffect(async () => ...) are not supported, ' +
'but you can call an async function inside an effect.\n' +
' in App (at **)',
'Warning: useEffect function must return a cleanup function or nothing.\n\n' +
'It looks like you wrote useEffect(async () => ...) or returned a Promise.',
]);
});