GithubHelp home page GithubHelp logo

useUserMedia about react-use HOT 5 OPEN

xvjg avatar xvjg commented on April 18, 2024
useUserMedia

from react-use.

Comments (5)

streamich avatar streamich commented on April 18, 2024

Looks like could be a useful addition to this library.

from react-use.

xvjg avatar xvjg commented on April 18, 2024

Ok wait... I actually found a problem.

I profiled my application and turns out a MediaStream saved in a component state will trigger a re-render every time there is a new frame in the video feed. This is very bad for the performance...

I guess the library shouldn't promote bad practice :p

from react-use.

streamich avatar streamich commented on April 18, 2024

Could removing stream prevent re-renders:

    useEffect(_ => {
        navigator.mediaDevices.getUserMedia(constraints)
            .then(setStream)
            .catch(errorCallback)
    }, []); // 👈 no stream

But you would also need to check your component was no unmounted when you set the stream.

Something like:

import {useRefMounted} from 'react-use';

        const refMounted = useRefMounted();
        navigator.mediaDevices.getUserMedia(constraints)
            .then((stream) => {
              if (refMounted.current) setStream(stream);
            })

from react-use.

xvjg avatar xvjg commented on April 18, 2024

Could removing stream prevent re-renders

This simply halved the number of re-renders lol. My project structure is such that there's an App component which stores a MediaStream object, and puts it in a video.

I'll try a useVideo implementation, but that would be opinionated. Lemme check.

from react-use.

xvjg avatar xvjg commented on April 18, 2024

Could removing stream prevent re-renders

This worked... No re-renders after that. I tried this hook on a fresh test app. Turns out my video-chat react project has some different problem :p Here's the fresh application where I tried the hook: https://gitlab.com/vixrant/useusermedia

You can use this snippet. I don't know typescript otherwise I would have sent a Pull Request!

And I noticed that I have to pass an empty array to useEffect. I can't let it be undefined. Turns out it's related to this. It's very weird and I hope they make it more intuitive.

function useUserMedia(constraints, errorCallback=(() => undefined)) {
    const [stream, setStream] = useState();

    useEffect(() => {
        navigator.mediaDevices.getUserMedia(constraints)
            .then(setStream)
            .catch(errorCallback)
    }, []);

    return stream;
}

from react-use.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.