--- myst: html_meta: "description": "Add event handlers to show or hide the answer in FaqItem component." "property=og:description": "Add event handlers to show or hide the answer in FaqItem component." "property=og:title": "Use Event Handlers" "keywords": "Plone, training, exercise, solution, React" --- (event-handlers-label)= # Use Event Handlers ## Toggle Function To show or hide the answer, we will add a toggle function to the component {file}`FaqItem.jsx`. ## Exercise Write the toggle handler which will toggle the `isAnswer` state variable and set the new state using the `setAnswer` function: ````{dropdown} Solution :animate: fade-in-slide-down :icon: question ```{code-block} jsx :emphasize-lines: 1-3 :lineno-start: 8 :linenos: true const toggle = () => { setAnswer(!isAnswer); }; ``` ```` ## Click Handler To call the newly created `toggle` function, we will add an `onClick` handler to the question: ```{code-block} jsx :emphasize-lines: 3-5 :lineno-start: 12 :linenos: true return (
{props.answer}
}{props.answer}
}{props.answer}
}