此代码给出TypeError:无法读取未定义的属性“ bind” FirebaseAppImpl。(匿名函数)[作为数据库] 在线错误 this.databaseref = 看起来像是来自火力发源地
从'react'导入React,{Component}; 从“ ../../PostEditor/components/PostEditor”导入PostEditor;
import Post from '../../Post/component/Post';
export default class ThreadDisplay extends Component{
constructor(props) {
super(props);
this.databaseRef = this.props.database().ref().child('posts');
this.addPost = this.addPost.bind(this);
this.updateLocalState = this.updateLocalState.bind(this);
this.state = {
posts:[],
}
}
componentWillMount() {
const{updateLocalState}=this;
this.databaseRef.on('child_added', snapshot => {
const response = snapshot.val();
updateLocalState(response);
});
}
addPost(postBody) {
const postToSave = {postBody};
this.databaseRef.push().set(postToSave);
}
updateLocalState(response)
{
const posts = this.state.posts;
const brokenDownPost= response.postBody.split(/[\r\n]/g);
posts.push(brokenDownPost);
this.setState({
posts: posts,
})
}
render() {
return(
<div>
{
this.state.posts.map((postBody, idx) => {
return (
<Post key={idx} postBody={postBody} />
)
})
}
<PostEditor addPost={this.addPost} />
</div>
);
}
}