So I am re-writing a component with hooks, and I ran into an interesting challenge, I need to mimick some old behaviour of componentWillReceiveProps
with the useEffect
hook.
My old code looks like:
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);
}
}
You see, I am initiating a const
based on nextProps, then in the if
statement i do a couple checks based on that nextVal, now, I know that we can specify a second argument to useEffect
to run it only when a prop changes, but what about those checks, how can i implement something similar to nextProps
?