mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Make useEffect(async) warning more verbose (#14327)
* Make useEffect(async) warning more verbose * Nit
This commit is contained in:
+11
-3
@@ -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.',
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user