Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
useEffect(() => {
if (steps.step2 && !requestKey) {
const timeoutId = setTimeout(() => startProcess(), 500)
return () => clearTimeout(timeoutId)
}, [steps, requestKey, startProcess])
eslint complains me error Expected to return a value at the end of arrow function consistent-return
for return () => clearTimeout(timeoutId)
Because when i comment this an error disappears.
// return () => { clearTimeout(timeoutId); }
How to solve this, any ideas?
If you're returning in at least one branch, you need to always return a value to follow the rule. It doesn't have to be a function, it just has to be something other than the standalone statement return
. This works too:
useEffect(() => {
if (steps.step2 && !requestKey) {
const timeoutId = setTimeout(() => startProcess(), 500)
return () => clearTimeout(timeoutId)
return undefined;
}, [steps, requestKey, startProcess])
Also note that setTimeout(() => startProcess(), 500)
simplifies to setTimeout(startProcess, 500)
.
–
When i add this return () => {}
at the end of useEffect(() => {...})
Now eslint doesn't complains about this.
useEffect(() => {
if (steps.step2 && !requestKey) {
const timeoutId = setTimeout(() => startProcess(), 500)
return () => clearTimeout(timeoutId)
+ return () => {}
}, [steps, requestKey, startProcess])
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.