Video is lost when trying to render component conditionally!?

Description
In a ReactJS app, I have a component that renders the host video only:

const VideoSingle = ({ mediaStream, user, width, height }) => {
  const videoRef = useRef()

  const renderVideo = useCallback(
    async (canvas, stream, u) => {
      try {
        if (!u.bVideoOn) {
          stream.stopRenderVideo(canvas, u.userId)
          return
        }

        await stream.renderVideo(
          canvas,
          u.userId,
          width,
          height,
          0,
          0,
          VideoQuality.Video_720P
        )
      } catch (error) {
        console.log(error)
      }
    },
    [height, width]
  )

  useEffect(() => {
    if (!mediaStream || !videoRef.current || !user) return

    renderVideo(videoRef.current, mediaStream, user)
  }, [videoRef, mediaStream, user])

  return (
    <canvas
        width={width}
        height={height}
        className='video-canvas'
        ref={videoRef}
        style={{ width, height }}
    />
  )
}

I’m rendering this component conditionally like this:

{slide === 'facilitator' && (
    <VideoSingle
               mediaStream={mediaStream}
              user={host}
              height={480}
              width={640}
            />
 )}

The video is visible on the first render, but after I hide and then show the component again, the video is lost. I checked all the parameters are same as in the first render and the code is passing the try block but the video is missing.

Browser Console Error
No errors in the console.

Which Web Video SDK version?
1.1.7

Hey @jetonthaci.88

Thanks for your feedback.

Try to keep the canvas element, use CSS style to hide the element when it doesn’t need to be shown. Frequently creating and destroying canvas will lead to ‘webgl context lost’, which is the cause of black video.

Thanks
Vic

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.