--- myst: html_meta: "description": "Refactor the app.js and create a new component so that we can use the markup." "property=og:description": "Refactor the app.js and create a new component so that we can use the markup." "property=og:title": "Convert To A Reusable Component" "keywords": "Plone, trainings, SEO, React, component, exercise, solution" --- (reusable-component-label)= # Convert To A Reusable Component ## Create A Reusable component To reuse the markup of a FAQ item, we will split up the code. The app component will contain just the data of the FAQ item and will render a newly created subcomponent called `FaqItem`. The data is passed to the subcomponent using properties. In the `FaqItem` component, you will have access to the properties with `props.question`. The {file}`App.js` code will be changed to: ```{code-block} jsx :emphasize-lines: 2,7-27 :linenos: true import "./App.css"; import FaqItem from "./components/FaqItem"; function App() { return ( ); } export default App; ``` ````{dropdown} Differences :animate: fade-in-slide-down :icon: question ```dpatch --- a/src/App.js +++ b/src/App.js @@ -1,31 +1,30 @@ import "./App.css"; +import FaqItem from "./components/FaqItem"; function App() { return ( ); } ``` ```` ## Exercise Create the `FaqItem` component in a newly created folder called `components` which renders the same output. Also move all the styling of the view to {file}`components/FaqItem.css`. ````{dropdown} Solution :animate: fade-in-slide-down :icon: question {file}`components/FaqItem.jsx` ```{code-block} jsx :linenos: import "./FaqItem.css"; const FaqItem = (props) => { return (
  • {props.question}

    {props.answer}

  • ); }; export default FaqItem; ``` ```` ## Property Validation React has a builtin mechanism to validate the properties being passed in into a component. When incorrect values are passed, you will receive a warning in the console. In the above example, you have to add an extra import: ```jsx import PropTypes from "prop-types"; ``` And the following static property to the function to validate the properties: ```jsx FaqItem.propTypes = { question: PropTypes.string.isRequired, answer: PropTypes.string.isRequired, }; ``` If you now add a third empty `` to {file}`App.js`, errors of missing properties on this component call will be reported in the JavaScript console of your browser. ```{code-block} jsx :emphasize-lines: 2,13-16 :linenos: true import "./FaqItem.css"; import PropTypes from "prop-types"; const FaqItem = (props) => { return (
  • {props.question}

    {props.answer}

  • ); }; FaqItem.propTypes = { question: PropTypes.string.isRequired, answer: PropTypes.string.isRequired, }; export default FaqItem; ```