【发布时间】:2016-07-11 17:31:56
【问题描述】:
我将 React 与 react-router 和 react-redux 一起使用。 当一个组件挂载时,我会派发从数据库中获取数据。 我的父组件有这个
const TopComp = React.createClass({
componentWillMount(){
this.props.dispatch(getData(this.params.id))
},
render() {
<ChildComp data={this.props.data} />
}
})
function mapStateToProps(state){
return {data:state.data}
}
export default connect()(TopComp)
我的子组件如下所示:
const ChildComp = React.createClass({
getInitialState() {
console.log(this.props.data)
//empty for the first time but gets data other times
}
render(){
console.log(this.props.data) //prints undefined
return(
{this.props.data} //gives the data
)
}
})
还有我的 TopComp 更新,点击它的父组件上的链接。它接收不同的 params.id 并为每个参数获取数据。
【问题讨论】:
标签: reactjs react-router redux react-redux react-router-redux