javascript - React.js 中这三种语法有什么区别?
问题描述
我是反应新手。它可能比 React 问题更多的是 JS 问题。当我为此部分以三种不同的方式编写代码时,它们都可以正常工作,在控制台中显示“ID 为 1”。
1
<Post click={() => this.postSelectedHandler(post.id)}/>
2
<Post click={() => {this.postSelectedHandler(post.id)}}/>
3
<Post click={() => {return this.postSelectedHandler(post.id)}}/>
我的问题是为什么它们都工作正常。特别是#3 返回函数,我不明白为什么它的工作方式与其他两个相同。关于 #1 和 #2,有无 {} 都无所谓?
原始代码(与#1 相同)
postSelectedHandler = id => {
console.log("ID is " + id);
};
render() {
const posts = this.state.posts.map(post => {
return (
<Post click={() => this.postSelectedHandler(post.id)}/>
);
});
return (
<div>
<section>{posts}</section>
</div>
);
}
谢谢您的帮助。
解决方案
推荐阅读
- javascript - CORS 策略阻止了对 fetch 的访问
- vba - 为什么在 VBA 中运行按预期工作的代码时出现运行时错误“76”-找不到路径?
- python - 将txt文件转换为字典列表
- mingw - msys64,mingw:意外标记 `(' 附近的语法错误
- rascal - 如何在 Rascal 中初始化包含表达式列表的元组
- android - 我们可以通过多少种方式在代码中访问 UI 组件
- azure - 注册具有应用程序权限的应用程序时“支持的帐户类型”的重要性
- node.js - 如果对象存在,Express Validator 检查嵌套字段
- python - 将列表值分配给字典
- javascript - 如何添加预先输入值的动态字段?