简体   繁体   English

useEffect 和 ESlint 穷举-deps 规则

[英]useEffect and ESlint exhaustive-deps rule

I'm currently stuck on how to build my logic without having any warning about exhaustive-deps in my useEffect .我目前坚持如何构建我的逻辑,而在我的useEffect中没有任何关于exhaustive-deps警告。

My goal is to track navigation (enter page date, leave page date and location) on location change.我的目标是在位置更改时跟踪导航(输入页面日期、离开页面日期和位置)。

I am using useLocation() of react-router-dom and useLastLocation() of react-router-last-location .我正在使用react-router-dom useLastLocation() useLocation() react-router-last-location useLastLocation() 。

const location = useLocation()
const lastLocation = useLastLocation()

const [date, setDate] = React.useState(new Date())

React.useEffect(() => {
  const end = new Date()
  API.sendData({ start: date, end, location: lastLocation })
  setDate(end)
}, [location, lastLocation])

This is working fine but my useEffect dependencies array should contains date to not have exhaustive-deps warning, but adding it, will make infinite loops.这工作正常,但我的useEffect依赖项数组应该包含没有exhaustive-deps警告的date ,但是添加它会产生无限循环。

What is the correct way to do it?正确的方法是什么?

The useState dispatch also allows you to provide a function that accepts the current value as an argument rather than simply a value. useState 调度还允许您提供一个 function,它接受当前值作为参数而不是简单的值。 That way you could avoid the date as a dependency.这样您就可以避免将date作为依赖项。

const location = useLocation()
const lastLocation = useLastLocation()

const [date, setDate] = React.useState(new Date())

React.useEffect(() => {
  setDate((currentDate) => {
    const end = new Date();
    API.sendData({ start: currentDate, end, location: lastLocation });
    return end;
  });
}, [location, lastLocation]);

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

相关问题 useEffect 依赖数组和 ESLint Exclusive-deps 规则 - useEffect dependency array and ESLint exhaustive-deps rule 这个详尽的 eslint 规则是否正确? - Is this exhaustive-deps eslint rule correct? useEffect 详尽-deps 规则令人困惑 - useEffect exhaustive-deps rule is confusing 使用 eslint Exclusive-deps 响应订阅/取消订阅的 useEffect 依赖项 - React useEffect dependencies for subscribe/unsubscribe with eslint exhaustive-deps useEffect 与 recyclerlistview (React Native) 和穷举-deps - useEffect with recyclerlistview (React Native) and exhaustive-deps 在响应中添加 useEffect 的正确方法来调用一次以满足 eslint react-hooks/exhaustive-deps? - Proper way to add useEffect in react to call once to satisfy eslint react-hooks/exhaustive-deps? useEffect 中的 Eslint 详尽 deps 警告 - Eslint exhaustive deps warning in useEffect 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) 如何使用React Hooks进行获取; ESLint强制执行“穷举下降”规则,这会导致无限循环 - How to do fetch with React Hooks; ESLint enforcing `exhaustive-deps` rule, which causes infinite loop React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM