useEffect和ESlint详尽跌落规则

时间:2020-08-06 09:49:46

标签: reactjs eslint eslint-plugin-react-hooks

我目前在如何建立自己的逻辑的同时,对自己的exhaustive-deps中的 useEffect 没有任何警告。

我的目标是在位置更改时跟踪导航(输入页面日期,离开页面日期和位置)。

我正在使用useLocation()中的react-router-domuseLastLocation()中的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警告,但添加警告将导致无限循环。< / p>

正确的方法是什么?

1 个答案:

答案 0 :(得分:1)

useState分派还允许您提供一个函数,该函数接受当前值作为参数,而不仅仅是一个值。这样,您可以避免将public bool CanChangeWord作为依赖项。

<TextBox Name="Word"
           Text="{Binding SelectedWordModel.Word}">

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TextChanged">
            <i:InvokeCommandAction Command="{Binding WordModelChangedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

</TextBox>