简体   繁体   中英

Change font-awesome icon onclick reactjs

I'm creating a navbar with React. I use burger menu and I want to change the bar menu to times on click currently I have following:

const {icons, setIcon} = useState('faBars');
...
<FontAwesomeIcon 
   icon={icons}
   onClick={() => {setIcon({faTimes})}}
/>

But obviously it doesn't work. I was wondering how can I correct the code.

Try passing the new value as a string as the following:

<FontAwesomeIcon 
   icon={icons}
   onClick={() => {setIcon('faTimes')}}
/>

The main problem could be you tried to pass as an object - setIcon({faTimes}) - instead of a string based on the initial value in your useState .

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