0
我使用componentDidMount来从火力点的数据,并设置与获取数据阵营路由器
componentDidMount() {
this.state.firebase.database().ref().on("value", function(snapshot) {
let data = snapshot.val()
// Authors
this.setState({authors: data.Author})
组件状态,则在渲染方法,我有这个
{
console.log(this.state.authors)
}
<Route path="/author/:authorId" component={AuthorPage} authors={this.state.authors} />
内AuthorPage路线道具未通过组件我有这个
render() {
console.log(this.props.route)
return (....
控制台输出是
Object {authorId: Object} // This is for this.state.authors
Object {path: "/author/:authorId", authors: undefined, component: function} // this is for this.props.route
正如您所看到的,this.state.authors有一个值,但在传递给组件作为路由道具时变为undefined。这从来没有发生过我,即使在与其他路线相同的应用程序。
在'this.setState({authors:data.Author})'之前执行'console.log(data.Author)'并检查返回的值。你有什么错误吗? –
不,状态正确分配,我检查了React开发工具。问题发生在路由器 –
同样的方式在其他地方工作?我可以提出另一种解决方案。 –