Reputation: 1824
I have the following component:
const CallScreen = () => {
const [callEnded, setCallEnded] = useState(false);
const updateTimerColor = () => {
setCallEnded(true);
};
const clock = useTimer(
callStartTime,
callLength,
callTimeOut,
timerReady,
connectionState,
updateTimerColor,
);
Then in useTimer
I want to call updateTimerColor
when certain conditions are met. The problem is is that when the state updates, the hook is called again and because the conditions under which updateTimerColor
is called are the same, the component infinitely re-renders.
How can I update state in CallScreen
from the hook without creating an infinite loop?
TIA.
Upvotes: 0
Views: 17
Reputation: 810
You should also include how you call updateTimerColor but maybe checking if callEnded changed value since last call before calling setCallEnded might prevent the infinite loop.
const updateTimerColor = () => {
//No need to set it again if the value did not change
if(!callEnded) {
setCallEnded(true);
}
};
Upvotes: 1