Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
Delay and Reclicking for Showing User media

**PROBLEM: **

I am trying to take user media once the user clicks on the button, ' enter the room ', and after that, his video (user media ) will be shown,

but when I click on the button place for user media video is occupied and the button gets shifted but no video appears and when I click again, then only the video is shown

import React, { useRef, useState } from 'react'

import styles from './userVideo.module.css'

function UserVideo() {
    const video = useRef();

    const [stream, setStream] = useState()
    



    const constraints = {
        audio: true,
        video: {
            width: 500,
            height: 300
        }
    }
    const getUserMedia = () => {

        
        alert(" getUser Media is called ")
        navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then((stream) => {
          
            setStream(stream)
            video.current.srcObject = stream


        })
        


    }







    return (
        <div className={styles.userVideoOuterContainer} >
           


            <button onClick={getUserMedia} className={styles.enterRoomButton}>
                Enter Room
            </button>
            {stream && <video  muted ref={video} autoPlay style={{ width: "300px" }} />}

        </div>
    )
}

export default UserVideo

SCREEN SHOTS enter image description here enter image description here

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The video be rendered before ref video.current set stream so the first time click to button video.current.srcObject = stream not work. You try let

video.current.srcObject = stream to useEffect has stream is dependencies like below code. Option 1:

const getUserMedia = () => {
    navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then((stream) => {
        setStream(stream)
    })
}
useEffect(() => {
  if(video.current){
    video.current.srcObject = stream
  }
}, [stream])

Option 2: You can let the condition to render video to CSS

<video  muted ref={video} autoPlay style={{ width: "300px", display: display: stream ? "block" : "none" }} />
almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!