I have a VideoPlayer component with crop functionality that's not working well after migration to functional over class component.
The VideoPlayer has a isVideoPlaying state (useState). It also contains a function toggleOrChangeIsVideoPlaying:
const togglePlayPauseVideo = (toPlay) => {
if (toPlay !== undefined) {
if (toPlay) {
playVideo()
} else {
pauseVideo()
}
} else {
if (!isVideoPlaying) {
playVideo()
} else {
pauseVideo()
}
}
}
It renders:
<div>
<Crop onPlayPauseVideo={togglePlayPauseVideo} ...restofTheProps>
<Video ...someProps />
</Crop>
</div>
Using useEffect & console.log() I verified that 'togglePlayPauseVideo' function is changing (and causing a bug), probably because VideoPlayer is re-rendered.
I've tried wrapping 'togglePlayPauseVideo' with useCallback. The problem is that it must have 'isVideoPlaying' state as a dependency (otherwise there's another bug), but when it does, it changes again more than it should.
Any ideas how to break this cycle?
BTW 1: 'isVideoPlaying' state is needed to keep track of the actual element state, that changes in playVideo() and pauseVideo() via ref.
BTW 2: VideoPlayer worked ok when it was a class component.