我有一个组件,它有一个道具传递给它。
似乎这个道具是 undefined
到了 componentDidMount()
被称为。
这是我的组件。
export class BookInventoryDetail extends React.Component {
constructor(props) {
super(props)
this.state = {
book_inventories: []
}
}
componentDidMount() {
console.log(this.props) // the prop book_id is undefined.
Book.fetchInventories(this.props.book_id).then(book_inventories => {
this.setState({book_inventories})
})
}
render() {
return (
<div>
</div>
)
}
}
我这样做是正确的,还是book_id没有被定义,而我认为它应该在父组件中被定义?
要么是我在这个组件中做错了什么,要么是问题存在于父组件中,而这个prop没有在我认为应该被设置的时候被设置。
所有的道具都应该在 componentDidMount
生命周期方法。我想你应该检查父组件中的book_id道具。是否一切正常?可能是你没有传递这个值。