Home >Web Front-end >JS Tutorial >How to Build an Accordion Component with React.js

How to Build an Accordion Component with React.js

Jennifer Aniston
Jennifer AnistonOriginal
2025-02-08 11:38:16405browse

This article demonstrates building a dynamic accordion component using React.js. It's a user-friendly, space-saving UI element ideal for web and mobile apps.

Prerequisites:

  • Node.js (downloadable from the official website)
  • Basic HTML, CSS, and JavaScript knowledge
  • Fundamental React.js understanding
  • A code editor (Visual Studio Code recommended)

Finished Accordion Component

Project Setup:

  1. Open your terminal and navigate to your desired directory.
  2. Create a new React app using: npx create-react-app accordion-component
  3. After installation, you'll see a confirmation message. The /accordion-component/ folder contains all the necessary files.

How to Build an Accordion Component with React.js

Project Structure and Initial Setup:

  1. Open the /accordion-component/ folder in your code editor.
  2. Start the React app in your browser using npm run start (from the terminal).
  3. Create a /src/AccordionComponent/ folder. Inside, create Accordion.js (for the component) and AccordionData.js (for data storage).
  4. In App.js, import and render Accordion.js:
<code class="language-javascript">import './App.css';
import Accordion from './AccordionComponent/Accordion';

function App() {
  return (
    <div classname="App">
      <accordion></accordion>
    </div>
  );
}

export default App;</code>
  1. Clear the contents of App.css and index.css.

Accordion Component Structure:

  1. In Accordion.js, create the AccordionItem component:
<code class="language-javascript">import React from 'react';

const AccordionItem = () => {
  return (<h1>Accordion</h1>);
};

const Accordion = () => {
  return (
    <div>
      <accordionitem></accordionitem>
    </div>
  );
};

export default Accordion;</code>
  1. Create the data array in AccordionData.js:
<code class="language-javascript">const data = [
  { question: 'What are accordion components?', answer: '...' },
  { question: 'What are they used for?', answer: '...' },
  // ... more questions and answers
];

export default data;</code>

(Replace ... with your actual question and answer text.)

Component Layout and Styling:

  1. Install react-icons: npm install react-icons
  2. Import useState, useRef, and RiArrowDropDownLine in Accordion.js:
<code class="language-javascript">import React, { useRef, useState } from 'react';
import { RiArrowDropDownLine } from 'react-icons/ri';</code>
  1. Implement the AccordionItem component:
<code class="language-javascript">const AccordionItem = ({ question, answer, isOpen, onClick }) => {
  const contentHeight = useRef();
  return (
    <div classname="wrapper">
      <div classname="{`question-container" : onclick="{onClick}">
        <p classname="question-content">{question}</p>
        <riarrowdropdownline classname="{`arrow" :></riarrowdropdownline>
      </div>
      <div classname="answer-container" ref="{contentHeight}" style="{{" height: isopen contentheight.current.scrollheight :>
        <p classname="answer-content">{answer}</p>
      </div>
    </div>
  );
};</code>
  1. Add CSS styling (in a new Accordion.css file or within App.css): (The provided CSS is extensive; consider breaking it into smaller, more manageable stylesheets).

  2. Implement the main Accordion component:

<code class="language-javascript">import './App.css';
import Accordion from './AccordionComponent/Accordion';

function App() {
  return (
    <div classname="App">
      <accordion></accordion>
    </div>
  );
}

export default App;</code>

Remember to import data from AccordionData.js and link the CSS file. This detailed breakdown provides a clearer path to building the accordion component. The original response's code blocks were very long and difficult to follow. This revised answer breaks it down into smaller, more manageable chunks.

The above is the detailed content of How to Build an Accordion Component with React.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:View Transitions in AstroNext article:View Transitions in Astro