Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从兄弟传递ref_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 从兄弟传递ref

Javascript 从兄弟传递ref,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在使用库,当我在App中为它创建ref而不是在每个屏幕中添加组件时,我只想将它的ref传递给我的路由器,然后使用它。 将其ref传递给兄弟姐妹的问题是-它们都一起呈现,因此当我传递ref时,它仍然未定义 render() { return ( <View style={styles.container}> <StatusBar /> <Router alertRef={this.alertRef}/> &

我正在使用库,当我在
App
中为它创建
ref
而不是在每个屏幕中添加组件时,我只想将它的
ref
传递给我的
路由器
,然后使用它。 将其
ref
传递给兄弟姐妹的问题是-它们都一起呈现,因此当我传递
ref
时,它仍然
未定义

render() {
  return (
    <View style={styles.container}>
      <StatusBar />
       <Router alertRef={this.alertRef}/>
       <DropdownAlert ref={ref => (this.alertRef = ref)} />
    </View>
  );
}
render(){
返回(
(this.alertRef=ref)}/>
);
}
您可以使用API创建
ref
。直到第一次渲染之后,
current
属性才会设置

class App extends React.Component {
  alertRef = React.createRef();

  render() {
    return (
      <View style={styles.container}>
        <StatusBar />
        <Router alertRef={this.alertRef} />
        <DropdownAlert ref={this.alertRef} />
      </View>
    );
  }
}
类应用程序扩展了React.Component{
alertRef=React.createRef();
render(){
返回(
);
}
}

我将传递组件本身,并在组件中镜像方法:

render() {
 return (
  <View style={styles.container}>
    <StatusBar />
    <Router alertRef={this}/>
    <DropdownAlert ref={ref => (this.alertRef = ref)} />
  </View>
 );
}

alertWithType(type, title, message) {
  this.alertRef.alertWithType(type, title, message);
}
render(){
返回(
(this.alertRef=ref)}/>
);
}
alertWithType(类型、标题、消息){
this.alertRef.alertWithType(类型、标题、消息);
}

我最终得到的解决方案是:

class App extends Component {

  constructor(props) 
  super(props)
  this.state = {alertRef:null}

  componentDidMount() {
    this.setState({alertRef:this.alertRef})
  }


  render() {
    return (
      <View style={styles.container}>
        <StatusBar />
        {this.state.alertRef && (
          <Router
            alertRef={this.state.alertRef}
            language={this.props.language}
          />
        )}
        <DropdownAlert ref={ref => (this.alertRef = ref)} />
      </View>
    );
  }

}
类应用程序扩展组件{
建造师(道具)
超级(道具)
this.state={alertRef:null}
componentDidMount(){
this.setState({alertRef:this.alertRef})
}
render(){
返回(
{this.state.alertRef&&(
)}
(this.alertRef=ref)}/>
);
}
}

你介意解释一下这里发生了什么吗?我有点困惑,它似乎对我不起作用(可能是因为我用错了),在
Router
中,我试着测试它和
console.log(this.props.alertRef)
查看传递给它的内容及其整个
App
实例,但没有
alertRef
,这很有意义,因为在您的示例中,您将
传递给了it@obiwankenoobi是的,但是您可以调用
this.props.alertRef.alertWithType(“error”、“error”、“test”)
,这是您想要实现的(不是吗?)是的。问题是当我试图调用
这个.props.alertRef.alertWithType(“error”,“error”,“test”
时,它出现了一个错误
无法读取未定义的属性“alertWithType”
@obiwankenoobi您不能在构造函数中调用它,它将只能在
componentDidMount