简体   繁体   English

如何在 React JS 中使用三元运算符将 Font Awesome Icons 与文本一起使用?

[英]How to use Font Awesome Icons with Text using ternary operator in React JS?

So I have a button and when I click the button the text changes and I managed to change the text after click using ternary operator and now I want to add icons from font awesome before the text.所以我有一个按钮,当我单击按钮时,文本会发生变化,并且我设法在单击后使用三元运算符更改文本,现在我想在文本之前添加来自 font awesome 的图标。 I used hooks to change the button text.我使用钩子来更改按钮文本。 Is there any way I can add these icons before the text?有什么办法可以在文本之前添加这些图标?

Code代码

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faPlus,faCheck } from '@fortawesome/free-solid-svg-icons'
const [disableBtn,setDisableBtn]=useState(false)
const [btnText,setBtnText]=useState(true)
<div class="buttons">
        <button className={disableBtn ? "addToClub disable":"addToClub"} onClick={()=>{handleTransfer(props.players);setBtnText(!btnText);setDisableBtn(true)}} >
            {btnText? <FontAwesomeIcon icon={faPlus} /> "Add to Club"   : (<FontAwesomeIcon icon={faCheck} /> "Player Already Added") }
        </button>
       
</div>

You can use react fragment to wrap the font awesome icon component and the text.您可以使用反应片段来包装字体真棒图标组件和文本。

Note: <>some content</> is shorthand for <React.Fragment>some content</React.Fragment> .注意: <>some content</><React.Fragment>some content</React.Fragment>的简写。

<button
   className={disableBtn ? "addToClub disable" : "addToClub"}
   onClick={() => {
      handleTransfer(props.players);
      setBtnText(!btnText);
      setDisableBtn(true);
   }}
>
   {btnText ? (
      <>
         <FontAwesomeIcon icon={faPlus} /> Add to Club
      </>
   ) : (
      <>
         <FontAwesomeIcon icon={faCheck} />
         Player Already Added
      </>
   )}
</button>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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