如何从React JS中的另一个组件获得refs
How to get refs from another component in React JS
App主组件中的代码如下:
class App extends Component {
componentDidMount(){
console.log(this.ref);
debugger;
}
render() {
return (
<div>
<Header/>
{this.props.children}
<Footer/>
</div>
);
}
}
与{this.props.children}
呈现的组件之一是主页,其中有引用的部分。
主页代码如下:
render(){
return (
<div className="homeMain">
<section ref="info"> <Info/> </section>
<section ref="contact"> <Contact /> </section>
</div>
);
}
我怎么能得到那些refs内的应用组件能够传递它们作为道具头?
我试图在应用组件的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);
React的主要思想是将props从父级向下传递给子级(甚至到更深层次的子级——我的意思是孙辈)
因此,当我们想让父类做一些被(或属于)子类触发的事情时,我们可以在父类中创建一个回调函数,然后把它作为props传递给子类
根据您的喜好,这是一个演示如何将回调函数向下传递到多个子级以及如何触发它们:
强制React容器刷新数据
重定向重新初始化类
在您的情况下,您可以从子组件访问引用,如下所示:(使用string for 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} />
)
}
}
子组件:
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>
)
}
}
ref
是每个this.props.children
的属性,因此您可以通过this.props.children
上的ref
属性访问父组件中的子组件ref
确保在componentDidMount
编辑:
如果可以,试试下面的代码:
var myChild= React.Children.only(this.props.children);
var clone = React.cloneElement(myChild, { ref: "myRef" });
相关文章:
- 如何在react js中将值从一个组件发送到另一个组件
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- Angular2将组件方法导入到另一个组件中
- 如何在另一个组件中获取指令/组件实例
- 无法将react组件作为属性传递给另一个组件
- 在React中将方法从一个组件移动到另一个组件,并且仍然可以在原始组件中使用
- 在另一个文件夹中显示React组件
- 如何从一个组件访问另一个组件的状态
- Vue.js从另一个组件获取数据
- Angular 2:在另一个组件上定义一个路由器,而不是引导的
- Angular2 如何使用 Javascript XMLHttpRequest 重定向到另一个组件
- (反应)如何在另一个组件中包含多个组件
- 如何在单文件组件中扩展另一个 VueJS 组件?(ES6 vue-loader)
- Vue.js 在另一个组件的一个组件中使用变量
- 在 React 中渲染一个组件中的另一个组件
- Vue.js 从另一个组件调用方法
- 反应如何更新另一个组件的状态
- 从另一个 ExtJS 自定义框架扩展组件
- 根据 Angular 2 中另一个组件中单击的内容更新一个组件中的数据
- React渲染一个组件,然后单击另一个React组件