[英]How can I fill icon color after onclick function using jquery in react js
Here is my code.Let me advice ,I want to change icon color after onclick function . 这是我的代码。请教我,我想在onclick函数后更改图标颜色。
<div class="timeline-footer">
<a
id="changeicon"
onClick={(event) => this.handleCount(post.id, event)}
class="m-r-25"
><i class="fa fa-heart-o"></i> {post.rating} Like</a>
</div>
This is my rating count function 这是我的评分计数功能
handleCount(id, event) {
event.classList.toggle("icon-heart");
fetch(this.state.url + '/rating/' + id, {
method: 'POST',
header: {
'Accept': 'applicatoin/json',
'Content-Type': 'application/json'
}
})
.then((response) => {
window.location.reload();
})
}
Do you want to change color after each click? 每次单击后是否要更改颜色? You can do it more 'react-way' without using jquery through state changing: 您可以通过状态更改而不使用jquery来进行更多的“反应式”操作:
changeColor = (color) => this.setState({iconColor: color});
getIconStyle = () => ({
color: this.state.iconColor
});
handleCount = (id, event) => {
event.classList.toggle("icon-heart");
this.changeColor("red"); // or anyway you want
fetch(this.state.url + '/rating/' + id, {
method: 'POST',
header: {
'Accept': 'applicatoin/json',
'Content-Type': 'application/json'
}
})
.then((response) => {
window.location.reload();
})
}
Inside render: 内部渲染:
<div className="timeline-footer">
<a
id="changeicon"
onClick={(event) => this.handleCount(post.id, event)}
style={this.getIconStyle()}
className="m-r-25"
>
<i className="fa fa-heart-o"></i> {post.rating} Like
</a>
</div>
Or change class instead of style if it suits your case 或者如果适合您的情况,请更改类别而不是样式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.