score:0
First create an array which looks like this:
const faqArray = [{question: 'What is my question', answer: 'this is my answer'}, {question: 'What is another question', answer: 'this is another answer'}]
This is your new FAQ array which stores your questions and answers. Place it in your file under import "./FaqPage.scss"
import { useState } from "react"
import "./FaqPage.scss"
const faqArray = [{question: 'What is my question', answer: 'this is my answer'}, {question: 'What is another question', answer: 'this is another answer'}]
Next go to where it has the following:
{[0, 1, 2, 3, 4, 5, 6, 7, 8].map
and replace the [0, 1, 2, 3, 4, 5, 6, 7, 8]
with
{faqArray.map
This is allowing your React component to loop
over the contents in the new array that we just made.
then where it says
Lorem Ipsum is simply dummy text of the printing?
replace it with {data.question}
This is replacing Lorem ipsum with your question that was stored in your FAQ array above.
Then where it has the paragraphs replace the paragraphs with <p>{data.answer}</p>
This is taking the answer you placed in your FAQ array and displaying it here. Note this will all be placed within one paragraph tag, if you want multiple paragraph tags it will take more complicated code. I would suggest this for now.
Now whenever you want to add a new question just add it to the faqArray like this {question: 'New question', answer: 'New answer'}
the question:
and answer:
are required as keys. don't change them unless you know what you are doing.
so with this new question it would look like this:
const faqArray = [{question: 'What is my question', answer: 'this is my answer'}, {question: 'What is another question', answer: 'this is another answer'}, {question: 'New question', answer: 'New answer'}]
After all is said and done your component should look like this:
import { useState } from "react"
import "./FaqPage.scss"
const faqArray = [{question: 'What is my question', answer: 'this is my answer'}, {question: 'What is another question', answer: 'this is another answer'}]
export default function FaqPage() {
const [faqRow, setfaqRow] = useState(0)
const faqToggle = (data) => {
console.log(data)
setfaqRow(data)
}
return (
<>
<section className="innerBAnner">
<div className="container">
<div className="row">
<div className="col-12">
<h1>Frequently asked questions</h1>
</div>
</div>
</div>
</section>
<section className="aboutPage">
<div className="aboutLayerTwo">
<div className="container">
<figure className="mb-xl-5 mb-xl-5">
<img src="img/img13.png" alt="" className="w-100 d-block" />
</figure>
<div className="row align-items-center pt-md-5"></div>
<div className="row">
{faqArray.map((data, ind) => (
<div className="col-md-12">
<div className="faq-row">
<div
className="d-flex align-items-end faq-head"
onClick={() => faqToggle(ind)}
>
<b>0{ind + 1}</b>
<h4 className="me-auto">
{data.question}
</h4>
<span>
{faqRow === ind ? (
<svg
width="19"
height="30"
viewBox="0 0 19 30"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M18.1189 14.6937L16.4545 12.95L4.40707 0.268555L0.731445 3.75596L11.1243 14.6937L0.731445 25.6315L4.40707 29.1189L16.4545 16.4374L18.1189 14.6937Z"
fill="#353535"
/>
</svg>
) : (
<svg
width="29"
height="18"
viewBox="0 0 29 18"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M14.4254 17.3875L16.1691 15.723L28.8506 3.67563L25.3632 -1.52439e-07L14.4254 10.3928L3.48764 -1.10865e-06L0.000232541 3.67563L12.6817 15.723L14.4254 17.3875Z"
fill="#353535"
/>
</svg>
)}
</span>
</div>
{faqRow === ind && (
<div className="faq-body">
<p>
{data.answer}
</p>
</div>
)}
</div>
</div>
))}
</div>
</div>
</div>
</section>
</>
)
}
Source: stackoverflow.com
Related Query
- How can I create and loop through a custom array within a react functional component?
- How to loop through an array an put create an array of objects using react and javascript?
- how can i loop through the array inside of an object. and render it in the react component
- How to loop through an array and display data within the object of that array, in react and javascript
- How do I create an array of objects in a React class component through a for loop to be mapped in a functional component?
- How to loop through properties of objects inside of an array within a React component?
- In React.js, how can I loop through an array of jsx elements and add attributes to them
- REACT: How to create a reusable custom hook with functions and effects that can change & react to changes in a host component's data
- Using javascript how can I loop through regex matches and split string into array of pieces divided by hyperlinks?
- how to loop through data and assign values to state array in react
- Javascript - How to loop through an array over and over again in React
- Loop through array in React and create elements (not list items) from it
- How do I loop through child array and check if component exists React Testing Library
- How can I scan through the list of string array items to identify which part matches regex expression for url and convert it to link w react typescri
- How to map through array of objects and create a new array of objects with a new added property using react and javascript?
- How to use for loop in react so I can loop over two array and return the object in second array if both match
- How to avoid very long paths and use absolute paths within a Create React App project?
- Loop through an array to create routes in react router
- Loop through array of file urls and download each one with React
- How can I keep my iterable value in loop with React and Redux?
- How I can loop through the similar objects in state and push them into object?
- How to iterate through a list of objects from api and render it to jsx page React functional component
- loop through an array and show single item at a time with dynamic time duration like setTimeout or SetInterval in react js
- How can I access the custom target attribute within material UI select options in React js
- How to loop through refs in React and get values?
- How to loop through an array of strings backwards x amount of times and start over when the end is reached?
- How can I create a configuration file for react and prevent webpack bundling it?
- Loop and return through 2d array in React
- How to loop an object that is inside an array and is inside an object? React
- How can I iterate through JSON arrays and show data in a table in React
More Query from same tag
- How to ensure an event listener is always called inside a ReactJS child component?
- Error trying to render an array of objects from the database
- CSS is not applied to Custom Tooltips's arrow
- React native-env file could not be found within the project
- updating HTML5 video src in reactjs
- Failed to get data: this.functionname is not a function (ReactJS)
- react markdown type for paragraph renderer?
- Fetching data with redux-thunk middle ware
- CSS hover/active touch feedback not working on mobile until react-select is shown
- How to mock async call in React functional component using jest
- Next.js "_framerMotion.motion.custom is not a function"
- Get height of tab bar on any device in React-Navigation
- How can I center the text in the headers for an AG-grid control?
- Jest mock react context
- Weather data is not displaying
- How to convert html string into plain text in React?
- How can I use subdirectories with react router?
- Filtering input fields in reactjs
- React Router Link not rendering as clickable button
- How to get Date and Time in Firebase Timestamp in react js
- Why won't my nested React components render?
- Converting ES5 Mixins to Higher Order Components
- Getting started with material-ui
- Cannot assign to read only property 'approved_by_customer_admin' of object '#<Object>' after updating the api data manually
- Decode a Uint8Array into a JSON
- Showing passport.js messages with flash
- I am trying to get the length of the text added and getting the error as Uncaught TypeError
- i have two components{addressone},{addresstwo} and want to add and delete {addresstwo} on button click inside {addressone} component
- Using subcollection from a query returned
- React & TypeScript: Functional Components that Are Not Arrow Functions