javascript - 为什么我必须在反应的事件处理程序中使用箭头函数?
问题描述
function ButtonIncrement(){
const [count,setCount] = useState(0);
render(){
<div>
<h3>
<button onClick={() => setCount(count+1)}>Increment me for fun</button>
<p>Count: {count} </p>
</h3>
</div>
}
}
在这个 onClick 按钮中,为什么简单的 putonClick={setCount(count+1)}
不起作用?我收到一个无限循环,看来我必须使用箭头函数。我怀疑它与“这个”有关。
解决方案
如果没有箭头功能,每次单击按钮都会再次呈现页面,这会设置状态,然后再次呈现,并设置状态,然后再次呈现。无限循环箭头函数在安装/更新时重新创建该函数一次。
推荐阅读
- python - python文件读取循环问题
- android - Expo React Native - 模拟器调试菜单快捷方式突然停止在 Windows 上工作
- r - 删除R数据帧中的相互引用行,即当一行中的(a,b)值作为(b,a)存在于同一数据帧的另一行中时
- java - 如何列出以特定文件格式结尾的 s3 存储桶中的文件?
- swift - 逐行解析 TXT 的最快方法
- python-3.x - 使用python从字符串中删除点(“。”)
- java - 如何启动内存数据库来测试 JPA 实体?
- javascript - axios 没有返回匹配参数对象查询字符串的数据,而是返回 vanila JS 中的所有数据
- r - 如何从数据框中提取任何给定数字并为其分配名称
- sql - 列值的 oracle 查找翻译