fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← React

React / 3 MIN READ

Expanding Menu

Accordion / expandable menu

From the original Fervor library. Examples may use older package versions.

Tutorial: Creating Expanding Menus/Topics in React

Introduction: Welcome to FrOnt3nd Tutorials! In this tutorial, we’ll explore how to create expanding menus or topics using React, a popular JavaScript library for building user interfaces. Expanding sections are a common UI pattern that can enhance user experience and engagement. By the end of this guide, you’ll be able to implement expandable sections in your React applications.

Table of Contents:

  1. Introduction
  2. Setting Up a React Project
  3. Creating the Expandable Component
  4. Adding State for Expansion
  5. Implementing Click Event Handling
  6. Bonus: Auto-Closing Expandables
  7. Conclusion

1. Introduction: React provides a powerful and efficient way to build dynamic user interfaces. In this tutorial, we’ll walk through the steps of creating expandable menus or topics in a React application. You’ll learn how to manage component state, handle click events, and add interactivity to your UI.

2. Setting Up a React Project: Before we begin, make sure you have Node.js and npm installed. To create a new React project, open your terminal and run the following commands:

npx create-react-app expanding-menus
cd expanding-menus
npm start

This will create a new React application and start the development server. Open src/App.js to get started.

3. Creating the Expandable Component: Let’s start by creating a Expandable component that represents an individual expandable section. Create a new file named Expandable.js in the src directory and add the following code:

import React, { useState } from 'react';
import './Expandable.css';

function Expandable(props) {
  const [expanded, setExpanded] = useState(false);

  return (
    <div className={`expandable ${expanded ? 'expanded' : ''}`}>
      <button className="expand-button" onClick={() => setExpanded(!expanded)}>
        {props.title}
      </button>
      <div className="content">
        {props.children}
      </div>
    </div>
  );
}

export default Expandable;

4. Adding State for Expansion: In the Expandable component, we’re using the useState hook to manage the expanded state. The initial value is set to false. The setExpanded function is used to update the state when the button is clicked.

5. Implementing Click Event Handling: We’re using the expanded state to conditionally apply the expanded class to the component. The button’s onClick event toggles the expanded state when clicked.

6. Bonus: Auto-Closing Expandables: To add auto-closing functionality, we need to manage the state of all Expandable components in a parent component. Open src/App.js and modify it as follows:

import React, { useState } from 'react';
import './App.css';
import Expandable from './Expandable';

function App() {
  const [openExpandable, setOpenExpandable] = useState(null);

  const handleExpand = (index) => {
    if (index === openExpandable) {
      setOpenExpandable(null);
    } else {
      setOpenExpandable(index);
    }
  };

  return (
    <div className="App">
      <Expandable title="Expandable 1" onClick={() => handleExpand(1)} expanded={openExpandable === 1}>
        {/* Content for Expandable 1 */}
      </Expandable>
      <Expandable title="Expandable 2" onClick={() => handleExpand(2)} expanded={openExpandable === 2}>
        {/* Content for Expandable 2 */}
      </Expandable>
      {/* Add more Expandable components */}
    </div>
  );
}

export default App;

7. Conclusion: Congratulations! You’ve successfully created expanding menus/topics in a React application. By following this tutorial, you’ve learned how to manage component state, handle click events, and create interactive UI patterns. React’s component-based architecture makes it easy to build dynamic and engaging user interfaces.

With the added bonus of auto-closing expandables, you’ve further improved the usability of your expandable sections. Continue exploring React’s capabilities and integrating these techniques into your projects to create feature-rich and user-friendly applications.

Keep your curiosity going.Explore more React →
287 TUTORIALS · 22 TOPICSREADY