尝试实现redux 的时候我突发奇想, 想试着获取一下嵌套的子元素, 但是发现 经过 connect 之后, this.props.children 就无法取到了
相关代码
Home.js
import React, { Component } from 'react'
// 这个是自己尝试实现react-redux
import { connect } from '../my-react-redux'
class Home extends Component {
constructor(props, context){
super(props, context)
}
componentDidMount(){
console.log(this.props.children) // underfind
}
render() {
const { num } = this.props
return (
<div>
{this.props.children}
</div>
)
}
}
const mapStateToProps = state=> ({
num: state.num
})
const mapDispatchToProps = dispatch => ({
})
// connect
export default connect(mapStateToProps,mapDispatchToProps)(Home)
App.js 的代码
class App extends Component {
render() {
return (
<div >
<Provider store={store}>
<Home>
<h3 style={{color:'red'}}>我是一个h3</h3>
</Home>
</Provider>
</div>
);
}
}
export default App;