Reactjs 反应:获取道具中的组件

Reactjs 反应:获取道具中的组件,reactjs,Reactjs,如何从父级调用呈现的子级组件的方法?基本上,我的应用程序使用一个switch语句来呈现3个选项卡,每个选项卡的根位置都包含一个MyComponent元素。我尝试使用props.children访问,但是当我调用child方法时,它抛出了一个错误 /*父级*/ changeChild(组件){ this.setState({component}); } callChildMethod(){ this.state.component.childMethod(); } render(){ 返回 }

如何从父级调用呈现的
子级
组件的方法?基本上,我的应用程序使用一个
switch
语句来呈现3个选项卡,每个选项卡的根位置都包含一个
MyComponent
元素。我尝试使用
props.children
访问,但是当我调用child方法时,它抛出了一个错误

/*父级*/
changeChild(组件){
this.setState({component});
}
callChildMethod(){
this.state.component.childMethod();
}
render(){
返回
}    
/*子对象*/
componentDidMount(){
this.props.changeChild(this.refs.root.props.children)//在此处传递给父级
}
renderContent(){
开关(此.props.tab){
案例0:
返回(
);
案例1:
返回(
);
案例2:
返回(
);
}
}
render(){
返回(
{this.renderContent()}
)
}

我建议采用不同的方法。 不要保存对处于状态的孩子的引用,只要在需要时请求引用即可。 而且,通过使用函数作为参考,它的错误更少

您需要在
上调用navigator.pop(),还是在div中使用ref
root调用navigator.pop()?
在下面的示例中,您可以访问div或组件

// parent

render() {
  return <Child ref={(ref) => this.childNode = ref} />
}

someMethod() {
  // make sure the component is mounted and you have a referece to child
  if (this.childNode) {
    // get child root node
    const childRootNode = this.childNode.getRootNode()
    // or get the comp node
    const compNode = this.childNode.getCompNode()
  }
}


/* child */
renderContent(setCompRef) {
    switch(this.props.tab) {
        case 0:
            return (
                <MyComponent ref={setCompRef} />
            );
        case 1:
            return (
                <MyComponent2 ref={setCompRef} />
            );
        case 2:
            return (
                <MyComponent3 ref={setCompRef} />
            );
    }
}

render() {
    return (
        <div ref={ref => this.rootNode = ref}>
            {this.renderContent(
              ref => this.compNode = ref // function that sets ref for component
            )}
        </div>
    )
}

// getters for the two refs
getRootNode() {
  return this.rootNode
}
getCompNode() {
  return this.compNode
}
//父对象
render(){
返回this.childNode=ref}/>
}
someMethod(){
//确保组件已安装,并且您有一个指向子组件的引用
if(this.childNode){
//获取子根节点
const childRootNode=this.childNode.getRootNode()
//或者获取comp节点
const compNode=this.childNode.getCompNode()
}
}
/*孩子*/
renderContent(setCompRef){
开关(此.props.tab){
案例0:
返回(
);
案例1:
返回(
);
案例2:
返回(
);
}
}
render(){
返回(
this.rootNode=ref}>
{this.renderContent(
ref=>this.compNode=ref//为组件设置ref的函数
)}
)
}
//两位裁判的接球手
getRootNode(){
返回此.rootNode
}
getCompNode(){
返回此.compNode
}

我建议采用不同的方法。 不要保存对处于状态的孩子的引用,只要在需要时请求引用即可。 而且,通过使用函数作为参考,它的错误更少

您需要在
上调用navigator.pop(),还是在div中使用ref
root调用navigator.pop()?
在下面的示例中,您可以访问div或组件

// parent

render() {
  return <Child ref={(ref) => this.childNode = ref} />
}

someMethod() {
  // make sure the component is mounted and you have a referece to child
  if (this.childNode) {
    // get child root node
    const childRootNode = this.childNode.getRootNode()
    // or get the comp node
    const compNode = this.childNode.getCompNode()
  }
}


/* child */
renderContent(setCompRef) {
    switch(this.props.tab) {
        case 0:
            return (
                <MyComponent ref={setCompRef} />
            );
        case 1:
            return (
                <MyComponent2 ref={setCompRef} />
            );
        case 2:
            return (
                <MyComponent3 ref={setCompRef} />
            );
    }
}

render() {
    return (
        <div ref={ref => this.rootNode = ref}>
            {this.renderContent(
              ref => this.compNode = ref // function that sets ref for component
            )}
        </div>
    )
}

// getters for the two refs
getRootNode() {
  return this.rootNode
}
getCompNode() {
  return this.compNode
}
//父对象
render(){
返回this.childNode=ref}/>
}
someMethod(){
//确保组件已安装,并且您有一个指向子组件的引用
if(this.childNode){
//获取子根节点
const childRootNode=this.childNode.getRootNode()
//或者获取comp节点
const compNode=this.childNode.getCompNode()
}
}
/*孩子*/
renderContent(setCompRef){
开关(此.props.tab){
案例0:
返回(
);
案例1:
返回(
);
案例2:
返回(
);
}
}
render(){
返回(
this.rootNode=ref}>
{this.renderContent(
ref=>this.compNode=ref//为组件设置ref的函数
)}
)
}
//两位裁判的接球手
getRootNode(){
返回此.rootNode
}
getCompNode(){
返回此.compNode
}
我想调用的方法我想调用的方法