如何从子 cComponent 访问 props?

发布于 2025-01-13 19:15:05 字数 2008 浏览 2 评论 0 原文

SipProvider来自npm包react-sip,它具有子组件可以访问的上下文(sip、call、startCall等)。 如何从子组件访问上下文。 (Dailer)

import React from 'react'
import { SipProvider } from '@evercall/react-sip'
import Dialer from '../components/Dialer'

const SipReact = () => {
    return (
        <SipProvider
        host="197.159.142.228"
        port={5060}
        pathname="/ws" // Path in socket URI (e.g. wss://sip.example.com:7443/ws); "" by default
        user="27329"
        password={'bB3JwU7i'} // usually required (e.g. from ENV or props)
        autoRegister={true} // true by default, see jssip.UA option register
        autoAnswer={false} // automatically answer incoming calls; false by default
        iceRestart={false} // force ICE session to restart on every WebRTC call; false by default
        sessionTimersExpires={120} // value for Session-Expires header; 120 by default
        debug={false} // whether to output events to console; false by default
        inboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        outboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        
      >
        <Dialer  />
      </SipProvider>
  )
}

export default SipReact

根据 npm 文档,子组件(Dialer)可以访问此上下文

{
  sip: sipType,
  call: callType,

  registerSip: PropTypes.func,
  unregisterSip: PropTypes.func,

  answerCall: PropTypes.func,
  startCall: PropTypes.func,
  stopCall: PropTypes.func,
}

Dailer 有此事件处理程序

const handleCall = () => {    
    const {
      startCall,
      sip: { status: sipStatus },
      call: { status: callStatus }
    } = props;

    if ( sipStatus !== sipType.SIP_STATUS_REGISTERED) {
      return;
    }
    if ( callStatus !== callType.CALL_STATUS_IDLE ) {
      return
    }
    startCall(number)
  }

如何从 Dailer 访问 SipProvider 上下文? 我尝试使用 props 但返回 undefied

SipProvider is from an npm package react-sip which has context (sip, call, startCall etc) that the child component has access to.
How do I access the context from the child component. (Dailer)

import React from 'react'
import { SipProvider } from '@evercall/react-sip'
import Dialer from '../components/Dialer'

const SipReact = () => {
    return (
        <SipProvider
        host="197.159.142.228"
        port={5060}
        pathname="/ws" // Path in socket URI (e.g. wss://sip.example.com:7443/ws); "" by default
        user="27329"
        password={'bB3JwU7i'} // usually required (e.g. from ENV or props)
        autoRegister={true} // true by default, see jssip.UA option register
        autoAnswer={false} // automatically answer incoming calls; false by default
        iceRestart={false} // force ICE session to restart on every WebRTC call; false by default
        sessionTimersExpires={120} // value for Session-Expires header; 120 by default
        debug={false} // whether to output events to console; false by default
        inboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        outboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        
      >
        <Dialer  />
      </SipProvider>
  )
}

export default SipReact

According to the npm docs, child components(Dialer) has access to this context

{
  sip: sipType,
  call: callType,

  registerSip: PropTypes.func,
  unregisterSip: PropTypes.func,

  answerCall: PropTypes.func,
  startCall: PropTypes.func,
  stopCall: PropTypes.func,
}

Dailer has this event handler

const handleCall = () => {    
    const {
      startCall,
      sip: { status: sipStatus },
      call: { status: callStatus }
    } = props;

    if ( sipStatus !== sipType.SIP_STATUS_REGISTERED) {
      return;
    }
    if ( callStatus !== callType.CALL_STATUS_IDLE ) {
      return
    }
    startCall(number)
  }

How do I access SipProvider context from Dailer ?
I tried using props but returns undefiened

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

默嘫て 2025-01-20 19:15:06

子组件(Dialer)应该有权访问startCall、call、sip

你为什么这么认为?除非有一些其他相关代码未显示,否则子组件无法访问这些道具。组件只能访问您显式传递给它们的 props。这意味着在 Dialer 组件实例化中,您需要像这样传入 value props:

<Dialer startCall={startCall}/>

In order for Dialer component to have access to startCall in its props.

编辑:

既然您澄清说您正在尝试从上下文访问这些值,那么问题的措辞是错误的,您没有访问道具,您正在访问上下文值,它们是不同的东西,因此需要不同的语法。

我快速查看了库存储库,它似乎使用的是旧/旧版 React 上下文,该上下文已在 React 17 中弃用。假设您使用的是较旧的 React 版本,您可以像这样访问上下文值:

import { sipPropType, callPropType } from '@evercall/react-sip';

const SipPhone = (props, context) => {

  const Sip = context;

  const handleCall = () => {
    console.log('handleCall', '\n' , Sip)
  }

  return (
    <div>
      <Button onClick={handleCall}>Call</Button>
    </div>
  );
}

SipPhone.contextTypes = {
  sip: sipPropType,
  call: callPropType,
  
  registerSip: PropTypes.func,
  unregisterSip: PropTypes.func,
  
  answerCall: PropTypes.func,
  startCall: PropTypes.func,
  stopCall: PropTypes.func,
}

export default SipPhone;

如果您使用的是 React 17 及更高版本,那么您就不走运了,这个库将无法工作,但您可以看看其他替代方案,例如 https://github.com/OpenTelecom/react-sip-phone。我看到有些人自愿更新该软件包,但我不指望它会很快完成。

the child component(Dialer) should have access to startCall, call, sip

Why do you think so? Unless there is some other relevant code that you have not shown, there is no way for the child component to have access to these props. A component only have access to the props you explicitly pass to them. That means in the Dialer component instantiation you need to pass in the value props like this:

<Dialer startCall={startCall}/>

In order for Dialer component to have access to startCall in its props.

EDIT:

Since you clarified that you are trying to access these values from a context, then the question is phrased wrongly, you are not accessing props, you are accessing context values, they are different things and therefore require different syntax.

I took a quick look at the library repository, and it seems that it is using the old/legacy React context, which has been deprecated in React 17. Assuming you are using an older React version, you can access the context values like this:

import { sipPropType, callPropType } from '@evercall/react-sip';

const SipPhone = (props, context) => {

  const Sip = context;

  const handleCall = () => {
    console.log('handleCall', '\n' , Sip)
  }

  return (
    <div>
      <Button onClick={handleCall}>Call</Button>
    </div>
  );
}

SipPhone.contextTypes = {
  sip: sipPropType,
  call: callPropType,
  
  registerSip: PropTypes.func,
  unregisterSip: PropTypes.func,
  
  answerCall: PropTypes.func,
  startCall: PropTypes.func,
  stopCall: PropTypes.func,
}

export default SipPhone;

If you are using React 17 and above then you are out of luck, this library won't work, but you can take a look at other alternatives like https://github.com/OpenTelecom/react-sip-phone. I see that some people have volunteered to update the package, but I wouldn't count on it being done anytime soon.

人心善变 2025-01-20 19:15:06

您必须像这样显式地将 props 传递给子组件

   const SipPhone = () => {
     return (
        <SipProvider
        host="197.159.142.228"
        port={5060}
        pathname="/ws" // Path in socket URI (e.g. wss://sip.example.com:7443/ws); "" by default
        user="27329"
        password={'bB3JwU7i'} // usually required (e.g. from ENV or props)
        autoRegister={true} // true by default, see jssip.UA option register
        autoAnswer={false} // automatically answer incoming calls; false by default
        iceRestart={false} // force ICE session to restart on every WebRTC call; false by default
        sessionTimersExpires={120} // value for Session-Expires header; 120 by default
        debug={false} // whether to output events to console; false by default
        inboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        outboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        
      >
            <Dialer startCall={startCall} sip={sip} call={call}/>
      </SipPhone>
  )
} 

You have to explicitly pass props to child component like this

   const SipPhone = () => {
     return (
        <SipProvider
        host="197.159.142.228"
        port={5060}
        pathname="/ws" // Path in socket URI (e.g. wss://sip.example.com:7443/ws); "" by default
        user="27329"
        password={'bB3JwU7i'} // usually required (e.g. from ENV or props)
        autoRegister={true} // true by default, see jssip.UA option register
        autoAnswer={false} // automatically answer incoming calls; false by default
        iceRestart={false} // force ICE session to restart on every WebRTC call; false by default
        sessionTimersExpires={120} // value for Session-Expires header; 120 by default
        debug={false} // whether to output events to console; false by default
        inboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        outboundAudioDeviceId={"default"} // default, or a deviceId obtained from navigator.mediaDevices.enumerateDevices()
        
      >
            <Dialer startCall={startCall} sip={sip} call={call}/>
      </SipPhone>
  )
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文