ash*_*tic 5 reactjs redux react-redux
最初一切都很好,我有一个类似的组件.这个
class A extends React.Component {
constructor(props) {
super(props);
this.childRef = null
}
componentDidMount() {
this.childRef = this.refs.b
// now I can call child function like this
this.childRef.calledByParent()
}
render(){
<B ref = "b"/>
}
}
Run Code Online (Sandbox Code Playgroud)
在其他文件中
class B extends React.Component {
calledByParent(){
console.log("i'm called")
}
render(){
<div> hello </div>
}
}
export default B
Run Code Online (Sandbox Code Playgroud)
到这里它工作正常,但当我做这样的事情 class B
export default connect(mapStateToProps, mapDispatchToProps)(B)
它不起作用.我从react-redux导入了connect
Shu*_*tri 10
connect()
接受option
作为第四个参数.在此选项参数中,您可以将flag设置withRef
为true.在此之后,您可以使用getWrappedInstance()
like 来访问refs函数
class A extends React.Component {
constructor(props) {
super(props);
this.childRef = null
}
componentDidMount() {
this.childRef.getWrappedInstance().calledByParent()
}
render(){
<B ref = {ref => this.childRef = ref}/>
}
}
class B extends React.Component {
calledByParent(){
console.log("i'm called")
}
render(){
<div> hello </div>
}
}
export default connect(mapStateToProps, mapDispatchToProps, null, {withRef: true})(B)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3031 次 |
最近记录: |