添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
腼腆的西瓜  ·  React-admin - ...·  1 月前    · 
大力的荒野  ·  Getting started with ...·  1 月前    · 
追风的花生  ·  Map in React js with ...·  1 月前    · 
温柔的保温杯  ·  How to Use the ...·  1 月前    · 
奔跑的凳子  ·  Loading in Rive Files ...·  1 月前    · 
鼻子大的牙膏  ·  docx4j word转pdf ...·  2 年前    · 
温文尔雅的小蝌蚪  ·  Android ...·  2 年前    · 
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).

ESLing should learn that a single return; in an untyped function will always return undefined which means that return; is exactly the same thing as return undefined;, both will return undefined. – Christos Lytras Apr 4, 2021 at 21:33

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])
                Returning an empty function is unnecessary to resolve this linter error. As @CertainPerformance's answer states, you can return undefined instead.
– timgcarlson
                Jun 24, 2022 at 16:48
        

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.