也就是说,加入我要创建一个handleSubmit 方法,我有两种写法:
写法1
export default class CommentAdd extends Component{
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
static propTypes = {
addComment: propTypes.func.isRequired
}
handleSubmit(){
let newComment = {}
newComment = {
name: 'lalla',
content:'我就试一下'
}
this.props.addComment(newComment); // 没有constructor中的bind过程,这里的this为undefined,所以会报错
}
写法2
export default class CommentAdd extends Component{
static propTypes = {
addComment: propTypes.func.isRequired
}
handleSubmit =()=>{
let newComment = {}
newComment = {
name: 'lalla',
content:'我就试一下'
}
this.props.addComment(newComment);
}