I converted my class-based component to a functional component and thought I had the conversion of the componentDidUpdate
method correct, but my linter is complaining, so I could use some clarification on what I'm getting wrong.
This is my componentDidUpdate
function on a class-based component:
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(),
});
}
This is what I'm attempting to change it to using useEffect()
on a functional component:
const saveInitialSession = () => {
setSessionProperties({
agentId,
customerId,
customerName,
startAt: moment().format(),
});
};
useEffect(() => {
if (hasUnlockBeenClicked && !startAt) {
saveInitialSession();
}
}, [hasUnlockBeenClicked]);
Eslint is telling me that the dependency array needs to include startAt
and saveInitialSession
, which doesn't make any sense to me. I only want it to run if the hasUnlockBeenClicked
has been changed.