我目前坚持如何构建我的逻辑,而exhaustive-deps在我的useEffect.
我的目标是在位置更改时跟踪导航(输入页面日期、离开页面日期和位置)。
我正在使用useLocation()ofreact-router-dom和useLastLocation()of react-router-last-location。
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])
这工作正常,但我的useEffect依赖项数组应该包含date没有exhaustive-deps警告,但添加它会产生无限循环。
正确的方法是什么?