简体   繁体   English

使用 eslint Exclusive-deps 响应订阅/取消订阅的 useEffect 依赖项

[英]React useEffect dependencies for subscribe/unsubscribe with eslint exhaustive-deps

I have a useEffect hook that should subscribe to geolocation updates when the component appears, and then unsubscribe when the component disappears.我有一个useEffect钩子,它应该在组件出现时订阅地理定位更新,然后在组件消失时取消订阅。 So I pass [] as the effect dependencies since I only want this to run on mount/unmount.所以我将[]作为效果依赖项传递,因为我只希望它在安装/卸载时运行。

import { useWatchPosition } from "@ionic/react-hooks/geolocation"
import React, { useEffect } from "react"

function SiteMap() {
  const { currentPosition, startWatch, clearWatch } = useWatchPosition()

  // Subscribe/Unsubscribe to geo location on component mount/unmount.
  useEffect(() => {
    startWatch()
    return clearWatch
  }, [])

  return <svg>{/* ... */}</svg>
}

Which causes eslint to warn:这导致 eslint 发出警告:

React Hook useEffect has missing dependencies: clearWatch and startWatch . React Hook useEffect 缺少依赖项: clearWatchstartWatch Either include them or remove the dependency array.eslint(react-hooks/exhaustive-deps)包含它们或删除依赖项array.eslint(react-hooks/exhaustive-deps)

So I changed it to this:所以我把它改成了这样:

  useEffect(() => {
    startWatch()
    return clearWatch
  }, [startWatch, clearWatch])

Which causes an infinite render loop.这会导致无限渲染循环。

I'm guessing the infinite loop is caused by the @ionic/react-hooks/geolocation library which is creating new functions every time useWatchPosition() is called, making the dependencies look stale.我猜测无限循环是由@ionic/react-hooks/geolocation库引起的,它在每次调用useWatchPosition()useWatchPosition()创建新函数,使依赖项看起来过时。

So should I just disable the check for this line via:所以我应该通过以下方式禁用对这一行的检查:

// eslint-disable-next-line react-hooks/exhaustive-deps

Or is there some way I'm missing to do the right thing here?或者我有什么方法可以在这里做正确的事情?

Reading the useWatchPosition source code , you can see that the functions are not created with useCallback , this means that they are regenerated whenever the hook is called.阅读useWatchPosition 源代码,你可以看到函数不是用useCallback创建的,这意味着只要调用钩子,它们就会重新生成。

You can store a reference to the functions in a ref , and use the ref.current to call the function:您可以在ref存储对函数的ref ,并使用ref.current调用该函数:

function SiteMap() {
  const { currentPosition, startWatch, clearWatch } = useWatchPosition()
  
  const startWatchRef = useRef(startWatch)
  const clearWatchRef = useRef()

  useEffect(() => {
    clearWatch.current = clearWatch // the updated clearWatch
  })

  // Subscribe/Unsubscribe to geo location on component mount/unmount.
  useEffect(() => {
    startWatchRef.current()
            
    return () => clearWatchRef.current()
  }, [])

  return <svg>{/* ... */}</svg>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ESLint 希望 setSate 作为 useEffect 的依赖,但这会导致无限循环(react-hooks/exhaustive-deps) - ESLint wants setSate as a dependency for useEffect but this causes an infinite loop (react-hooks/exhaustive-deps) 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps useEffect 和 &#39;react-hooks/exhaustive-deps&#39; linting - useEffect and 'react-hooks/exhaustive-deps' linting React Hook useEffect 缺少依赖项。 包括它们或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“roomID”和“sotreId”。 要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: 'roomID 'and 'sotreId'. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive 如何在React中使用钩子实现componentDidMount以使其符合EsLint规则“ react-hooks / exhaustive-deps”:“ warn”? - How to implement componentDidMount with hooks in React to be in line with the EsLint rule “react-hooks/exhaustive-deps”: “warn”? 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`? 我如何正确使用 useEffect 进行异步获取调用和反应? 反应钩子/详尽的deps - How do i properly use useEffect for a async fetch call with react? react-hooks/exhaustive-deps Eslint React Hooks错误:eslint-plugin-react-hooks用尽详尽的警告警告useEffect中的功能依赖项 - Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM