如何从React JS中的另一个组件获取refs

Bok*_*oky 12 javascript reactjs

主App组件中的代码如下:

class App extends Component {
  componentDidMount(){
      console.log(this.ref);
      debugger;
  }

  render() {

    return (
        <div>
            <Header/>
            {this.props.children}
            <Footer/>
        </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

呈现的组件之一{this.props.children}是HomePage,其中是带有refs的部分.

HomePage的代码如下:

render(){
     return (
        <div className="homeMain">
            <section ref="info"> <Info/> </section>

            <section ref="contact"> <Contact /> </section>
       </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

如何在App组件中获取这些引用,以便能够将它们作为道具传递给标头?

我正在尝试在App组件中的componentDidMount中执行此操作,但它console.log(this.refs)是空的.

有什么建议?

编辑

整个App组件:

import React, {Component} from 'react';
import Footer from './common/footer';
import Header from './common/header';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import * as actions from './components/homepage/login/authActions';

class App extends Component {
    componentDidMount(){
        console.log(this.props.children.refs);
        debugger;

    }

    render() {
        return (
            <div>
                <Header route={this.props.location.pathname}
                        language={this.props.language.labels}
                        authenticated={this.props.authenticated}
                        signoutAction={this.props.actions}
                        offsets={this.props.offsets}
                />
                {React.cloneElement(this.props.children, {
                    currentLanguage: this.props.language.labels,
                    authenticated: this.props.authenticated
                })}
                <div className="clearfix"/>
                <Footer currentLanguage={this.props.language.labels}/>
            </div>
        );
    }
}

function mapStateToProps(state, ownProps) {
    return {
        language: state.language,
        authenticated: state.auth.authenticated,
        offsets: state.offsets
    };
}

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(actions, dispatch)
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(App);
Run Code Online (Sandbox Code Playgroud)

thi*_*108 9

React 的主要思想是将 props 从父级向下传递给子级(甚至传递到更深层次的子级 - 我的意思是孙子)

因此,当我们希望父级执行由(或属于)子级触发的操作时,我们可以在父级中创建回调函数,然后将其作为道具传递给子级

根据您的喜好,这是关于如何通过多个子级向下传递回调函数以及如何触发它们的演示:

强制 React 容器刷新数据

重定向时重新初始化类

在您的情况下,您可以按如下方式访问来自子组件的 refs :(使用字符串作为 ref - 如您所述)

父组件:

import React, { Component } from 'react';
// import Child component here

export default class Parent extends Component {
  constructor(props){
    super(props);
    // ...
    this.getRefsFromChild = this.getRefsFromChild.bind(this);
  }    

  getRefsFromChild(childRefs) {
    // you can get your requested value here, you can either use state/props/ or whatever you like based on your need case by case
    this.setState({
      myRequestedRefs: childRefs
    });
    console.log(this.state.myRequestedRefs); // this should have *info*, *contact* as keys
  }    

  render() {
    return (
      <Child passRefUpward={this.getRefsFromChild} />
    )
  }  
}
Run Code Online (Sandbox Code Playgroud)

子组件:

import React, { Component } from 'react';

export default class Child extends Component {
  constructor(props){
    super(props);
    // ...
  }    

  componentDidMount() {
    // pass the requested ref here
    this.props.passRefUpward(this.refs);

  }    

  render() {
    return (
      <div className="homeMain">
        <section ref="info"> <Info/> </section>

        <section ref="contact"> <Contact /> </section>
      </div>          
    )
  }  
}  
Run Code Online (Sandbox Code Playgroud)


KOT*_*IOS 0

ref是每个组件的属性,因此您可以通过属性this.props.children访问父级中子组件的引用refthis.props.children

确保您在之后访问参考componentDidMount

编辑 :

如果可行,请尝试下面的代码集:

var myChild= React.Children.only(this.props.children);
var clone = React.cloneElement(myChild, { ref: "myRef" });
Run Code Online (Sandbox Code Playgroud)