Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda