简体   繁体   中英

How to render fontawesome as button in ReactJS

I am trying to render a button with fontawesome icon using the below snippet:

<div className="form-group  col-md-2">
   <input type="button" className="btn btn-default">
   <i className="fas fa-plus-circle"></i> add
   </input>
</div>

But I keep getting this error:

input is a void element tag and must neither have children nor use dangerouslySetInnerHTML .

What is the correct way to do it?

In React, <input /> cannot render any child elements. Instead, use button .

<div className="form-group col-md-2">
    <button type="button" className="btn btn-default">
        <i className="fas fa-plus-circle"></i> add
    </button>
</div>

Here is an example in Sandbox .

As the error message states with input element unfortunately you could not have any element inside.

One possible solution can be to use button instead of input element. By adding a CSS class and set the content property as in the below example, read from the documentation :

The content property is used with the ::before and ::after pseudo-elements, to insert generated content.

 .icon-content:after { content: '🔥 add'; }
 <button class="btn btn-default icon-content" />

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM