iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
⚛️

Building an FAQ Accordion! [HubSpot × React DAY 10]

に公開
2026/01/12

Created an FAQ with an Accordion! 【HubSpot × React DAY10】

2025-05-11 · Reina

Introduction 🌿

FAQ lists can easily become hard to read when there are many items.

Illustration of Charo trying to open an accordion (troubled face)
An accordion-style UI where "clicking a question reveals the answer" is a delight for both users and editors!
In this post, I created an interactive FAQ module using HubSpot's React modules + islands + RepeatedFieldGroup ✨

react-day10


What you will learn in this article ✏️

  • How to implement an accordion-style FAQ list
  • The relationship between useState state management and islands
  • How to freely add Q&A items using RepeatedFieldGroup

Conclusion 🎯

If you want to add an accordion UI with HubSpot CMS × React, islands are essential!
Furthermore, using RepeatedFieldGroup makes it convenient to freely increase questions and answers from the management screen 💡


How to do it 🛠️

Step 1: Use RepeatedFieldGroup in the field definition ✍️

index.jsx
export const fields = (
  <ModuleFields>
    <RepeatedFieldGroup
      name="faqs"
      label="FAQ List"
      occurrence={{ min: 1, max: 20, default: 3 }}
      default={[
        {
          question: 'Sample Question: Who is Charo?',
          answer: '<p>A cute little Scottish girl!</p>',
        },
      ]}
    >
      <TextField name="question" label="Question" default="Sample Question" required />
      <RichTextField name="answer" label="Answer (Rich Text)" default="<p>Sample Answer</p>" />
    </RepeatedFieldGroup>
  </ModuleFields>
);

Step 2: Create a client component with island support 📦

islands/FAQAccordionIsland.jsx

import React, { useState } from 'react';

/**
 * Component that holds the logic for the accordion display
 */
export default function FAQAccordionIsland({ faqs }) {
  const [openIndex, setOpenIndex] = useState(null);
  const toggle = (i) => setOpenIndex(openIndex === i ? null : i);

  return (
    <section className="faq-list" style={{ maxWidth: 800, margin: '0 auto' }}>
      {faqs?.map((faq, index) => (
        <div key={index} style={{ marginBottom: 16, border: '1px solid #ddd', borderRadius: 8, overflow: 'hidden' }}>
          <h3
            onClick={() => toggle(index)}
            style={{ margin: 0, padding: 12, cursor: 'pointer', backgroundColor: '#f7f7f7' }}
            aria-expanded={openIndex === index}
          >
            {faq.question}
          </h3>
          {openIndex === index && (
            <div style={{ padding: 12, borderTop: '1px solid #eee', backgroundColor: '#fff' }}>
              <div dangerouslySetInnerHTML={{ __html: faq.answer }} />
            </div>
          )}
        </div>
      ))}
    </section>
  );
}

// Point: Importing from the module with ?island enables hydration of this component on the client side 💡 

What are the mechanisms being used here?

  • We use useState to record the "index (number) of the open question".
    For example, if openIndex = 2, only the third Q&A is in the open state✨

  • The toggle(i) function provides the logic to close the same question if clicked again, or to open a different question if a new one is clicked.

  • Using openIndex === i to determine "whether this question is currently open" is the key point!

  • We use islands because this movement (state switching) needs to run via JavaScript on the client side 💡


Step 3: Hydrate to the client side with Island 🏝️

index.jsx
import { Island } from '@hubspot/cms-components';
// Import the part extracted as an Island component (the ?island is the key)
import FAQAccordionIsland from '../../islands/FAQAccordionIsland.jsx?island';

/**
 * HubSpot CMS Module: Using Island to hydrate the client component
 */
export function Component({ fieldValues }) {
    return (
      <Island module={FAQAccordionIsland} faqs={fieldValues.faqs} />
    );
  }

Important Notes ⚡


Summary ✨

Just by displaying your FAQs as an accordion, visibility and usability are significantly improved!
With the combination of HubSpot CMS × React × islands, you can create interactive UIs one after another.
It can be applied in various ways, such as for "Frequently Asked Questions" or "Glossaries," so please try making use of it💡

Charo smiling and pointing at the accordion FAQ

Discussion