这两个箭头函数有什么区别?
What is the diff between this two arrow functions?
基本上,我不明白为什么第二个箭头功能有效,而第一个无效。
//this one doesnt
this.setState = () => {
text: e.target.value,
};
//this one works
this.setState(() => ({
text: e.target.value,
}));
第一个是赋值,第二个执行执行。
第一个将箭头函数分配给 this.setState,并且由于 this 上存在一个名为 setState 的函数,它将被您的箭头函数覆盖。
第二个将箭头函数作为参数传递给 setState,setState 将在执行过程中的某个时刻调用您刚刚传递的函数。
您可以参考文档以更好地了解 setState 的作用。
基本上,我不明白为什么第二个箭头功能有效,而第一个无效。
//this one doesnt
this.setState = () => {
text: e.target.value,
};
//this one works
this.setState(() => ({
text: e.target.value,
}));
第一个是赋值,第二个执行执行。
第一个将箭头函数分配给 this.setState,并且由于 this 上存在一个名为 setState 的函数,它将被您的箭头函数覆盖。
第二个将箭头函数作为参数传递给 setState,setState 将在执行过程中的某个时刻调用您刚刚传递的函数。
您可以参考文档以更好地了解 setState 的作用。