admin管理员组文章数量:1430077
I have a one React ponent which contains iteration of div elements like below:
render(){
return(
<div className="col-md-12">
{this.state.pageOfItems.map(opration =>
<div className=" col-md-4 icondiv" key={opration.id}
onClick={this.selectOperation.bind(this,opration,true)}>
<FontAwesome name="square" style={{ ariaHidden:true, fontSize:'35px'}}/>
<span className="displayblock">{opration.name}</span>
</div>
)}
</div>
);
}
Now my question is When I my onClick method execute I want to change clicked div's square icon color. remaining icon's color should be same. Now when I clicked on another div's icon then same should happen. How can I achieve this. I know I can set css color to different but How can I identified particular div and change color?
I have a one React ponent which contains iteration of div elements like below:
render(){
return(
<div className="col-md-12">
{this.state.pageOfItems.map(opration =>
<div className=" col-md-4 icondiv" key={opration.id}
onClick={this.selectOperation.bind(this,opration,true)}>
<FontAwesome name="square" style={{ ariaHidden:true, fontSize:'35px'}}/>
<span className="displayblock">{opration.name}</span>
</div>
)}
</div>
);
}
Now my question is When I my onClick method execute I want to change clicked div's square icon color. remaining icon's color should be same. Now when I clicked on another div's icon then same should happen. How can I achieve this. I know I can set css color to different but How can I identified particular div and change color?
Share Improve this question asked Jun 27, 2017 at 22:03 realcodesrealcodes 2691 gold badge5 silver badges17 bronze badges 1- consider accepting the working answer! stackoverflow./help/accepted-answer.. you gain 2 rep points every time you accept an answer! – Rachel Gallen Commented Jun 28, 2017 at 0:06
1 Answer
Reset to default 5In your state, add a member 'selectedOperationId' and update it in the 'selectOperation' callback.
Then build the className of your div conditionally like
className={operation.id === this.state.selectedOperationId ? "selected col-md-4 icondiv" : "col-md-4 icondiv"}
and make the proper css for .selected class
本文标签: javascriptChange Color of Icon in ReactStack Overflow
版权声明:本文标题:javascript - Change Color of Icon in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745479129a2660099.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论