How can I export click event callbacks that use React state from within a component?

I’m building a Dashboard to visualize a few NLP experiments.

Here’s a snippet from the Dashboard component (.tsx) I’ve currently written:

import { Dispatch, SetStateAction, useEffect, useState } from "react";

import ControlGroup from "./ControlGroup";
import { DocumentResponse } from "../types/api";
import { ErrorType } from "../App";
import TextItem from "./TextItem";
import TextItemSkeleton from "./TextItemSkeleton";
import { buttonData } from "../lib/data";

type DashboardProps = {
  setError: Dispatch<SetStateAction<ErrorType>>;
};
const MAX_LOAD_DOCUMENTS = 3;

const Dashboard = ({ setError }: DashboardProps) => {
  const [documentsLoaded, setDocumentsLoaded] = useState(false);
  const [documents, setDocuments] = useState<Array<string>>(Array(3).fill(""));

  useEffect(() => {
    const handleLoadDocuments = async () => {
      ...
    };
    handleLoadDocuments();
  }, []);

  const handleToLowercase = async () => {
    try {
      setDocumentsLoaded(false);
      setError({
        message: `Converting documents into lowercase...`,
        type: "warning",
      });
      const response = await fetch(
        "http://127.0.0.1:5000/preprocess?" +
          new URLSearchParams({
            do: "lowercase",
          }).toString(),
      );
      const result: DocumentResponse = await response.json();
      setDocuments(result.documents);
      setDocumentsLoaded(true);
      setError({
        message: `All documents converted to lowercase.`,
        type: "success",
      });
    } catch (error) {
      setError({
        message: `Couldn't convert to lowercase: ${error}`,
        type: "error",
      });
    }
  };

  return (
    <main className="flex flex-1 flex-row gap-4 overflow-y-auto p-4">
      <section className="document-list flex-1 overflow-y-auto pr-4 pt-4">
        {documentsLoaded
          ? documents.map((text, index) => (
              <TextItem key={`text-${index}`} text={text} index={index} />
            ))
          : documents.map((_, index) => (
              <TextItemSkeleton key={`text-skeleton-${index}`} />
            ))}
      </section>
      <section className="document-controls flex-1">
        {buttonData.map((group, index) => (
          <ControlGroup
            key={`control-group-${index}`}
            groupTitle={group.groupTitle}
            buttons={group.buttons}
          />
        ))}
      </section>
    </main>
  );
};

export default Dashboard;

export type { DocumentResponse };

You might see that towards the end of the above component, I’m rendering an array of buttons (buttonData.map(...)).

Each button is actually found within another component called ControlGroup – but this is all ready from an array which looks like this:

const buttonData = [
  {
    groupTitle: "Text preprocessing",
    buttons: [
      { text: "To lowercase", disabled: false, onClick: () => {} },
      { text: "Remove punctuation", disabled: true, onClick: () => {} },
      { text: "Lemmatization", disabled: true, onClick: () => {} },
      { text: "Porter Stemmer", disabled: true, onClick: () => {} },
    ],
  },
  ...
]

There’s also a function called handleToLowercase() defined within the Dashboard.tsx component – this is relevant for later.


To each button, I’ve added an onClick attribute – which basically is the function that should run when that button is clicked.

What I want to do is specify handleToLowercase() as the onClick attribute’s value of that button in the buttonData array.


I initially exported the handleToLowercase callback and tried importing it from the script that contains the buttonData array – but this didn’t work since my callback uses state (i.e. setDocumentsLoaded, setError, etc.) which is only accessible from within the Dashboard component.

Also, I don’t think exporting such callbacks is a best practice + feel that there might be some use in a state management library here – but please correct me if I’m wrong.


Any ideas as to how I can render the buttons from the buttonData array, while also connecting each button to the respective callback within the Dashboard.tsx component which uses state?

Appreciate any help, thanks for taking the time to read my question until the end!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật