简体   繁体   English

未通过React-Router路由道具

[英]React-router route props not passed

I am using componentDidMount to fetch data from firebase and set the components state with fetched data 我正在使用componentDidMount从Firebase获取数据并使用获取的数据设置组件状态

  componentDidMount() {
  this.state.firebase.database().ref().on("value", function(snapshot) {
  let data = snapshot.val()

  // Authors
  this.setState({authors: data.Author})

then in render method I have this 然后在渲染方法中我有这个

{
    console.log(this.state.authors)
}
<Route path="/author/:authorId" component={AuthorPage} authors={this.state.authors} />

Inside AuthorPage component I have this 在AuthorPage组件内部,我有这个

render() {
    console.log(this.props.route)
    return (....

The console output is 控制台输出为

Object {authorId: Object} // This is for this.state.authors
Object {path: "/author/:authorId", authors: undefined, component: function} // this is for this.props.route

As you can see, this.state.authors has a value, but becomes undefined when passed to component as route props. 如您所见,this.state.authors有一个值,但是当作为路由道具传递给组件时变得不确定。 This has never happened to me before, even in the same app with other routes. 即使在与其他路线相同的应用中,这也从来没有发生过。

For some reason this solves the problem 由于某种原因,这解决了问题

  let authorsArray = data.Author
  let aths = this.state.authors
  for(var j in authorsArray) {
    aths[j] = authorsArray[j]
  }
  this.setState({authors: aths})

Anybody know why? 有人知道为什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM