【发布时间】:2019-06-09 00:26:17
【问题描述】:
我有一个 React Web 应用程序,我已经安装并导入了 FontAwesome 图标库。
https://github.com/FortAwesome/react-fontawesome
import { library } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faIgloo } from '@fortawesome/free-solid-svg-icons';
import { faStar } from '@fortawesome/free-solid-svg-icons';
library.add(faStar);
调用 onClick 时,我无法从对象中获取道具。
看起来当我点击图标时我触发了我的反应按钮并且道具被很好地传递了。如果我直接点击图标,会触发 onClick 但不会传递道具。
<button key={item.title} value={item.title} onClick={this.updateFavourites}>
<FontAwesomeIcon size="3x" icon="star" />
</button>
updateFavourites = event => {
console.log("IN UPDATE FAVOURITES" + event.target.value);
this.setState({
selectedFavourite: event.target.value
});
if (this.state.favourites.includes(event.target.value)){
// Find and remove item from an array
var i = this.state.favourites.indexOf(event.target.value);
if(i !== -1) {
this.state.favourites.splice(i, 1);
}
}
else{
this.state.favourites.push(event.target.value);
}
}
我做错了什么?我已经阅读了我能找到的与此问题相关的所有帖子。
感谢您的帮助
【问题讨论】:
标签: javascript reactjs onclick font-awesome