带有 FontAwesome 子项的按钮使按钮目标值未定义

Button with FontAwesome child makes the button target value undefined

我似乎想不出如何解决这个问题,如果你小时候点击一个带有 fontawesome 图标的 react-bootstrap 按钮,onclick target value 将未定义。

但是,如果您只在带有图标的文本外部单击但仍在按钮内部,则 onclick 处理程序仍然有效。

https://jsfiddle.net/utmcykcg/5/

问题似乎是 event.target 会根据您在按钮中单击的位置而改变。使用 event.currentTarget 应该会给你想要的结果。

onClick(e) {
  const btnValue = e.currentTarget.value
  alert(btnValue);
}

另请参阅:What is the exact difference between currentTarget property and target property in javascript