Use Event Handlers#
Toggle Function#
To show or hide the answer, we will add a toggle function to the component FaqItem.jsx
.
Exercise#
Write the toggle handler which will toggle the isAnswer
state variable and set the new state using the setAnswer
function:
Click Handler#
To call the newly created toggle
function, we will add an onClick
handler to the question:
12 return (
13 <li className="faq-item">
14 <h2 className="question" onClick={toggle}>
15 {props.question}
16 </h2>
17 {isAnswer && <p>{props.answer}</p>}
18 </li>
19 );