我试图从renderList中抓取数据并尝试在render()上使用该数据。 当我CONSOLE.LOGTypeError:无法读取BookDetails.render undefined的属性'book'
this.renderList()
以上是我找回数据。
于是就渲染() 我试着CONSOLE.LOG
console.log(this.renderList()[0].book);
上面的代码抛出
TypeError: Cannot read property 'book' of undefined at BookDetails.render
所以好像书是没有定义的渲染()不知道你怎么可以定义书籍属性render()。
renderList() {
return this.props.list.map((list) => {
console.log("list", list.book_id);
console.log("params", this.props.params.id)
if(list.book_id === parseInt(this.props.params.id)) {
console.log("true");
return {
book: list.book_id
}
}
})
}
render() {
const {post} = this.props;
const {user} = this.props;
const {list} = this.props;
const data= this.renderList()[0].list;
if(!post) {
return <div>Loading...</div>
}
if(user) {
console.log(this.renderList());
console.log(this.renderList()[0].book);
return (
<div>
<h1>Title: {post.title}</h1>
<h2>Pages: {post.pages}</h2>
<div>Reviews:</div>
<button
onClick={() => { this.props.addToMyPage(
{
userId: user.user.user_id,
bookId: post.book_id
}
)}}>
Add this to my page
</button>
</div>
)
}
之前,确保你有一个清单是这样的数据加载异步?它可能是它一开始不存在,然后再呈现。 –