I'm making a TimePicker like this
, I have another state is duration (number of hours from start to end), I made auto change end time when start or duration change function using useEffect().
It's work when I try to change the duration
but I'm got bug when I change start or end time here is my code:
useEffect(() => {
setEnd(moment(start).add(duration, 'hours'));
}, [start, duration]);
useEffect(() => {
setDuration(moment.duration(end.diff(start)).asHours());
}, [end, start]);
CodeSanBox Sample (it's difference with this question a bit, because I improved some logic about moment)
Update: Thanks for all your help, I did it, this is final code: CodeSanBox Final
