我将基于类的组件转换为功能组件,并认为我的componentDidUpdate
方法转换正确,但我的 linter 抱怨,所以我可以澄清一下我出错的地方。
这是我componentDidUpdate
在基于类的组件上的功能:
componentDidUpdate(prevProps) {
if (
prevProps.hasUnlockBeenClicked !== this.props.hasUnlockBeenClicked &&
this.props.hasUnlockBeenClicked &&
!this.props.startAt
) {
this.saveInitialSession();
}
}
saveInitialSession = () => {
const { setSessionProperties, agentId, customerId, customerName } = this.props;
setSessionProperties({
agentId,
customerId,
customerName,
startAt: moment().format(),
});
}
这就是我试图将其更改为useEffect()
在功能组件上使用的内容:
const saveInitialSession = () => {
setSessionProperties({
agentId,
customerId,
customerName,
startAt: moment().format(),
});
};
useEffect(() => {
if (hasUnlockBeenClicked && !startAt) {
saveInitialSession();
}
}, [hasUnlockBeenClicked]);
Eslint 告诉我依赖数组需要包含startAt
and saveInitialSession
,这对我来说没有任何意义。我只希望它hasUnlockBeenClicked
在更改后运行。