--- myst: html_meta: "description": "Add state to the App component." "property=og:description": "Add state to the App component." "property=og:title": "How To Use State In Your Component" "keywords": "Plone, trainings, SEO, React, component, exercise, solution" --- (state-label)= # How To Use State In Your Component ## Store Questions And Answers In The State To manipulate the FAQ, we will move all the data to the state of the component. The state of the component is the local state of that specific component. When the state changes, the component re-renders itself. We can initialize the state in our functional component body using the `useState` hook. ```{code-block} jsx :emphasize-lines: 6-15,19-21 :linenos: true import { useState } from "react"; import "./App.css"; import FaqItem from "./components/FaqItem"; function App() { const [faqList, setFaqList] = useState([ { question: "What does the Plone Foundation do?", answer: "The mission of the Plone Foundation is to protect and...", }, { question: "Why does Plone need a Foundation?", answer: "Plone has reached critical mass, with enterprise...", }, ]); return ( ); } export default App; ``` ````{dropdown} Differences :animate: fade-in-slide-down :icon: question ```dpatch --- a/src/App.js +++ b/src/App.js @@ -1,30 +1,24 @@ +import { useState } from "react"; import "./App.css"; import FaqItem from "./components/FaqItem"; function App() { + const [faqList, setFaqList] = useState([ + { + question: "What does the Plone Foundation do?", + answer: "The mission of the Plone Foundation is to protect and...", + }, + { + question: "Why does Plone need a Foundation?", + answer: "Plone has reached critical mass, with enterprise...", + }, + ]); + return ( ); } ``` ```` ## Exercise To save space in the view, we want to show and hide the answer when you click on the question. Add a state variable to the `FaqItem` component, which keeps the state of the answer being shown or not, and adjust the render method to show or hide the answer. ````{dropdown} Solution :animate: fade-in-slide-down :icon: question {file}`components/FaqItem.jsx` ```{code-block} jsx :emphasize-lines: 6,10 :linenos: true import { useState } from "react"; import "./FaqItem.css"; import PropTypes from "prop-types"; const FaqItem = (props) => { const [isAnswer, setAnswer] = useState(false); return (
  • {props.question}

    {isAnswer &&

    {props.answer}

    }
  • ); }; FaqItem.propTypes = { question: PropTypes.string.isRequired, answer: PropTypes.string.isRequired, }; export default FaqItem; ``` ```dpatch --- a/src/components/FaqItem.jsx +++ b/src/components/FaqItem.jsx @@ -1,11 +1,13 @@ +import { useState } from "react"; import "./FaqItem.css"; import PropTypes from "prop-types"; const FaqItem = (props) => { + const [isAnswer, setAnswer] = useState(false); return (
  • {props.question}

    -

    {props.answer}

    + {isAnswer &&

    {props.answer}

    }
  • ); }; ``` ````