I am trying to stream a video, but when i set 'myVideo.current.srcObject = currentStream' as in the code below, I recieve an error 'Cannot set properties of undefined (setting 'srcObject')'
const myVideo = useRef({});
const userVideo = useRef({});
const connectionRef = useRef();
useEffect(() => {
navigator.mediaDevices
.getUserMedia({ video: true, audio: true })
.then((currentStream) => {
setStream(currentStream);
myVideo.current.srcObject = currentStream;
});
socket.on("me", (id) => {
setMe(id);
});socket.on("callUser", ({ from, callerName, signal }) => {
setCall({ isReceivedCall: true, from, callerName, signal });
});
}, []);```
When using
const myVideo = useRef(), the value ofmyVideo.currentmay beundefineduntil the ref has been attached to something.You may need to check that
myVideo.currentexists before doing any operations on it.You could do this with
ifstatements or optional chainingifoptional chaining
Both will only execute once the
myVideo.currenthas a value