Home > Blockchain >  React FAQ component expands all items instead of just one
React FAQ component expands all items instead of just one

Time:05-18

So I've created a FAQ section in my React/Nextjs app and I've almost gotten it to work. The only problem is that any time I click a single item to expand it, all the items end up expanding. I'm using state and a ternary operator to toggle it on and off but I need it to only expand the item that I'm currently clicking on.

Code

import styles from '../styles/FAQs.module.css'
import {useRef, useEffect, useState} from 'react';

const FAQs = () => {
  const [isToggled, setIsToggled] = useState(false);

  const toggler = () => {
    isToggled ? setIsToggled(false) : setIsToggled(true);
  }

  return (
    <div class={styles.container}>
      <div class={styles.accordion}>
        <div class={styles.accordionItem}>
          <button id="accordion-button-1" aria-expanded={isToggled} onClick={toggler}>
            <span class={styles.accordionTitle}>Why is the moon sometimes out during the day?</span>
            <span class={styles.icon} aria-hidden="true"></span>
          </button>
          <div class={styles.accordionContent}>
            <p>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
              incididunt ut labore et dolore magna aliqua. Elementum sagittis vitae et leo duis ut.
              Ut tortor pretium viverra suspendisse potenti.
            </p>
          </div>
        </div>

        <div class={styles.accordionItem}>
          <button id="accordion-button-1" aria-expanded={isToggled}>
            <span class={styles.accordionTitle}>Why is the moon sometimes out during the day?</span>
            <span class={styles.icon} aria-hidden="true"></span>
          </button>
          <div class={styles.accordionContent}>
            <p>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
              incididunt ut labore et dolore magna aliqua. Elementum sagittis vitae et leo duis ut.
              Ut tortor pretium viverra suspendisse potenti.
            </p>
          </div>
        </div>
      </div>
    </div>
  )
}

export default FAQs

CodePudding user response:

The problem with your code is that you have multiple FAQs but you only have one state and one function to handle all the FAQs so when you click on one FAQ, all the FAQs will be triggered because you've one state for all of them.

So create an individual state for all the FAQs.

import styles from '../styles/FAQs.module.css'
import {useRef, useEffect, useState} from 'react';

const FAQs = () => {
  const [isToggled1, setIsToggled1] = useState(false);
  const [isToggled2, setIsToggled2] = useState(false);


  const toggler1 = () => {
    isToggled1 ? setIsToggled1(false) : setIsToggled1(true);
  }

  const toggler2 = () => {
    isToggled2 ? setIsToggled2(false) : setIsToggled2(true);
  }

  return (
    <div class={styles.container}>
      <div class={styles.accordion}>
        <div class={styles.accordionItem}>
          <button id="accordion-button-1" aria-expanded={isToggled1} onClick={toggler1}>
            <span class={styles.accordionTitle}>Why is the moon sometimes out during the day?</span>
            <span class={styles.icon} aria-hidden="true"></span>
          </button>
          <div class={styles.accordionContent}>
            <p>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
              incididunt ut labore et dolore magna aliqua. Elementum sagittis vitae et leo duis ut.
              Ut tortor pretium viverra suspendisse potenti.
            </p>
          </div>
        </div>

        <div class={styles.accordionItem}>
          <button id="accordion-button-1" aria-expanded={isToggled2} onClick={toggler2}>
            <span class={styles.accordionTitle}>Why is the moon sometimes out during the day?</span>
            <span class={styles.icon} aria-hidden="true"></span>
          </button>
          <div class={styles.accordionContent}>
            <p>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
              incididunt ut labore et dolore magna aliqua. Elementum sagittis vitae et leo duis ut.
              Ut tortor pretium viverra suspendisse potenti.
            </p>
          </div>
        </div>
      </div>
    </div>
  )
}

export default FAQs

CodePudding user response:

You need to store toggle status for all the sections/accordions separately in a map/object or an array or multiple state variables and toggle the one that is clicked on.

import styles from '../styles/FAQs.module.css'
import {useRef, useEffect, useState} from 'react';

const FAQs = () => {
const [accordionStatus, setAccordionStatus] = useState({ accordion1: false, accordion2: false });

const toggler = (name) => {
  setAccordionStatus({ ...accordionStatus, [name]: !accordionStatus[name]);
}

   return (
<div class={styles.container}>
  <div class={styles.accordion}>
    <div class={styles.accordionItem}>
      <button id="accordion-button-1" aria-expanded={accordionStatus.accordion1} onClick={() => toggler(accordion1)}>
        <span class={styles.accordionTitle}>Why is the moon sometimes out during the day?</span>
        <span class={styles.icon} aria-hidden="true"></span>
      </button>
      <div class={styles.accordionContent}>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
          incididunt ut labore et dolore magna aliqua. Elementum sagittis vitae et leo duis ut.
          Ut tortor pretium viverra suspendisse potenti.
        </p>
      </div>
    </div>

    <div class={styles.accordionItem}>
      <button id="accordion-button-1" aria-expanded={accordionStatus.accordion2} onClick={() => toggler(accordion2)>
        <span class={styles.accordionTitle}>Why is the moon sometimes out during the day?</span>
        <span class={styles.icon} aria-hidden="true"></span>
      </button>
      <div class={styles.accordionContent}>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
          incididunt ut labore et dolore magna aliqua. Elementum sagittis vitae et leo duis ut.
          Ut tortor pretium viverra suspendisse potenti.
        </p>
      </div>
    </div>
  </div>
</div>
 )}

export default FAQs
  • Related