【发布时间】:2021-01-22 15:30:53
【问题描述】:
我有两个在 jsx 中单击按钮时调用 onRemove 方法的示例。然而,虽然没有编译错误,但示例 2 并没有在屏幕上呈现任何内容,这意味着它基本上是错误的。
示例 2 有什么问题,为什么我们需要传递另一个匿名函数而不是在按钮单击时调用 {onRemove(item.id)}?
eg1:
function onRemove(id) {
//
}
<button type="button" onClick={() => onRemove(item.id)}>
eg2:
function onRemove(id) {
//
}
<button type="button" onClick={onRemove(item.id)}>
编辑: 类似地,onChange 的工作方式类似于上面的 eg2。
const handleChange = (search) => {
//
};
<input
id="search"
type="text"
onChange={handleChange}/>
【问题讨论】:
标签: reactjs react-native jsx