简体   繁体   English

无需单击按钮,函数方法就会在反应中调用

[英]Without clicking the button the function method is calling in react

i'm calling intiateVideoCall method first and i have button called turnOff but its loading first with out clicking the button i don't undestand the issue here.我首先调用intiateVideoCall方法,我有一个名为turnOff按钮,但它首先加载而不单击按钮我不理解这里的问题。 please help me thanks in advance请帮助我提前谢谢

const constraints = {'video': true, 'audio': true};
var localStream,providerName ;
  
  function turnOff(){
   alert('calling....')        
   } 


async function playFromLocalCamera() {
    try {
        const stream = await navigator.mediaDevices.getUserMedia(constraints);
        const localElement = document.querySelector('video#localVideo');
        localElement.srcObject = stream;
        return stream;
    } catch(error) {
        console.error('Error opening video camera.', error);
    }
}


async function intiateVideoCall() {
    providerName = getProviderName().replace('/provider/','');
    localStream = await playVideoFromLocalCamera();
    socket.emit('provider_join',providerName);   
  }




      <div className="md-form" id="customerWaiting">
          <video id="localVideo" width="80%" height="80%" autoPlay playsInline controls={false} onLoad={intiateVideoCall()}/>
          <video id="remoteVideo" width="80%" height="80%" autoPlay playsInline controls={false} />
          <button type="button" id="video" class="btn btn-outline-primary"  onClick={turnOff()}>
            <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-camera-video" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
              <path fill-rule="evenodd" d="M0 5a2 2 0 0 1 2-2h7.5a2 2 0 0 1 1.983 1.738l3.11-1.382A1 1 0 0 1 16 4.269v7.462a1 1 0 0 1-1.406.913l-3.111-1.382A2 2 0 0 1 9.5 13H2a2 2 0 0 1-2-2V5z"></path>
            </svg>
          </button>
      </div>                  
    

Instead of代替

<video id="localVideo" width="80%" height="80%" autoPlay playsInline controls={false} onLoad={intiateVideoCall()}/>
...
<button type="button" id="video" class="btn btn-outline-primary"  onClick={turnOff()}>

Use

<video id="localVideo" width="80%" height="80%" autoPlay playsInline controls={false} onLoad={intiateVideoCall}/>
...
<button type="button" id="video" class="btn btn-outline-primary"  onClick={turnOff}>

When we use onLoad={intiateVideoCall()} or onClick={turnOff()} then function will get executed even without click event.当我们使用onLoad={intiateVideoCall()}onClick={turnOff()}即使没有点击事件,函数也会被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM