目前正在尝试学习与 firebase 的react,并不断遇到一些小障碍。
截至目前,我正在尝试在新页面中呈现来自选定项目的数据。索引页包含以下内容:
renderPosts(){
return Object.keys(this.state.posts).map((post, index)=>(
<div key={index} className="thumbnail">
<h2>
<Link to={`/view/posts/${post}`}>
{this.state.posts[post].title}
</Link>
</h2>
<p>{this.state.posts[post].body}</p>
</div>
));
}
render() {
return (
<div>
<div>
{this.renderPosts()}
</div>
<View/>
</div>
)
}
如您所见,它将该帖子的 ID 附加到链接中,所以我现在想要做的是回到 firebase 并在新页面中单独使用该 ID 呈现项目的相应数据。
ViewPost.js 文件如下:
constructor(props){
super(props);
this.state = {
title: '',
body: '',
};
}
componentDidMount(){
database.on('value', snapshot => {
this.setState({
post: snapshot.val()
});
this.props.match.params.postId;
});
}
render() {
return (
<div >
{this.state.props.title}
</div>
)
}
你能告诉我我做错了什么以及如何做吗?