RocketChat / EmbeddedChat

An easy to use full-stack component (ReactJS) embedding Rocket.Chat into your webapp
https://www.npmjs.com/package/@embeddedchat/react
107 stars 214 forks source link

Bug Report: Missing Dependency in useEffect #574

Open parth26nath opened 2 months ago

parth26nath commented 2 months ago

Bug Report: Missing Dependency in useEffect

Description

The useMediaRecorder hook in packages/react/src/hooks/useMediaRecorder.js has a potential bug related to the dependency array in the useEffect hook. The useEffect hook is missing some dependencies, which can lead to unexpected behavior and potential bugs in the code execution.

Steps to Reproduce

  1. Use the useMediaRecorder hook in a React component without providing all required dependencies.
  2. Trigger actions that should update the missing dependencies during component lifecycle changes.

Expected Behavior

The useEffect hook should include all dependencies necessary for its proper functioning and synchronization with component state and props.

Actual Behavior

The useEffect hook in useMediaRecorder does not include all required dependencies, such as constraints, onStop, recorder, stream, and videoRef. This can result in incorrect behavior when these dependencies change, leading to potential bugs like memory leaks, improper cleanup, or unexpected side effects.

Actual Behavior Screenshot

Impact

Recommendation

  1. Update the dependency array in the useEffect hook within useMediaRecorder to include all necessary dependencies.
  2. Ensure proper error handling and cleanup logic in related hooks and functions, such as useUserMedia.
  3. Test the updated code thoroughly to verify that the bug is resolved and that the useMediaRecorder hook functions as expected under various scenarios.

Code Snippet (Updated useEffect)


useEffect(() => {
  async function start() {
    const _stream = await getStream();
    chunks.current = [];
    const _recorder = new MediaRecorder(_stream);
    _recorder.start();
    setRecorder(_recorder);
    _recorder.addEventListener('dataavailable', (event) => {
      chunks.current.push(event.data);
    });
    _recorder.addEventListener('stop', () => {
      onStop && onStop(chunks.current);
    });
  }

  return () => {
    if (recorder) {
      recorder.stop();
      stream.getTracks().forEach((track) => track.stop());
    }
  };
}, [constraints, getStream, onStop, recorder, stream, videoRef]);