简体   繁体   中英

UP and Down Arrow Icons functionality not working in Reactjs

Basically I need to show and Hide the div, Up and Down Arrow Icons should change accordingly. As of Now only UP arrow Icon is working and displaying the div, but when i try to hide the div, it is not working....I did like this and its not working, What wrong in my code? Can Anyone help me in this?

Here is my Code

const [expand, setExpand] = useState(false); 

<div onClick={() => setExpand(true)}> 
    Accordion 
    <i className={`fas fa-chevron-up ${!expand ? 'fas fa-chevron-down' : ''}`}></i>
</div>
{expand && <div className="Accordion-Section"></div>}

You can set the className like this

<div onClick={() => setExpand(!expand)}> 
    Accordion 
    <i className={`fas ${expand ? 'fa-chevron-up' : 'fa-chevron-down'}`} />
</div>
{expand && <div className="Accordion-Section"></div>}

The click handler is setting the state true everytime.

You should change it to <div onClick={() => setExpand( expand)}> .

you should change "i" tag to this:

<i className={ ${?expand: "fas fa-chevron-down" "fas fa-chevron-up"} }>

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