reactjs - Reactjs - 如何将字体真棒图标附加到 ref
问题描述
我正在尝试使用 appendChild 将 fontawesome 图标添加到 react 中的 ref。我得到错误:
无法在“节点”上执行“附加孩子”:参数 1 不是“节点”类型。
使用 append 而不是 appendChild 只能添加文本。
类示例扩展 React.Component {
handle = (e) => {
e.target.appendChild('<i className="fas fa-minus-circle"/>')
}
render() {
return (
<div className="container">
<button onClick={this.handle}>CLICK HERE</button>
</div>
)
}
}
解决方案
考虑稍微改变你的方法。您可以使用下面的代码实现您期望的相同结果,并且在 React 中使用状态和条件 JSX 更为常见:
class Example extends React.Component {
state = { images: [] }
handle = () => {
this.setState({ images: [...images, 'fas fa-minus-circle'] });
}
render() {
return (
<div className="container">
<button onClick={this.handle}>CLICK HERE</button>
{this.state.images.map((image, index) => {
<i key={index} className={image} />
})}
</div>
);
}
}
推荐阅读
- python - 用两个条件替换重复项
- swift - 如何在 Scroll 上增加 UICollectionView 的大小
- vue.js - 在数据库中看不到我的数据,但我可以使用 vue.js 插入数据
- python - 从数据框中提取数据
- python - 会话中没有循环的递归更新张量
- android-asynctask - 如何延迟 onPostExecute() 方法的执行直到 api 调用完成
- jupyter-notebook - Jupyter 笔记本 mathjax 内联符号与文本重叠
- c# - VS2019 测试资源管理器由于焦点错误导致点击失误
- java - Java从表中获取分层数据并生成树
- database - 如何正确映射孩子 - @Manytoone-> Parent 关系,其中父母在 JPA 中使用 Hiberate 有一个 @EmbeddedID