所以我正在重新编写一个带有钩子的组件,我遇到了一个有趣的挑战,我需要模仿一些componentWillReceiveProps
带有useEffect
钩子的旧行为。
我的旧代码如下:
componentWillReceiveProps(nextProps: Props) {
const prevLateVal = get(`lateMinutes[${bookingId}].value`, this.props);
const nextLateVal = get(`lateMinutes[${bookingId}].value`, nextProps); //see here,
//we use next props
if (typeof nextLateVal !== 'undefined' && prevLateVal !== nextLateVal) {
client.connect(bookingId, nextLateVal === null ? 0 : nextLateVal);
}
}
你看,我正在启动一个const
基于 nextProps,然后在if
语句中我基于 nextVal做一些检查,现在,我知道我们可以指定第二个参数useEffect
来运行它,只有当props改变时,但那些呢检查,我怎样才能实现类似的东西nextProps
?