Table data not getting updated until i refresh the page

The issue I’m encountering is that when I add data to the database through a modal component, the new data doesn’t immediately appear in the table. It only shows up after I refresh the page. I attempted to implement a refresh key logic to refetch the component, but it isn’t working. I want the new data to be reflected in the table as soon as it’s added through the modal, without requiring a manual refresh. The datatable component is where I’m calling both the modal and the table.

import React, { useState, useEffect } from "react";
import ReviewFeature from "./ReviewFeature";
import ArtistModal from "./ArtistModal";
import Dropdown from "./Dropdown";
import Layout from "../../Components/Layout";
import { fetchReviewArtist, searchArtist } from "../../Services/Api";

const ReviewDataTable = () => {
  const [modalIsOpen, setModalIsOpen] = useState(false);
  const [dropdownVisible, setDropdownVisible] = useState(false);
  const [searchTerm, setSearchTerm] = useState("");
  const [filteredItems, setFilteredItems] = useState([]);
  const [artists, setArtists] = useState([]);
  const [refreshKey, setRefreshKey] = useState(0);

  useEffect(() => {
    const fetchArtists = async () => {
      try {
        const response = await fetchReviewArtist();
        console.log("Fetched artists:", response);
        setArtists(response);
        console.log("set artist", setArtists);
      } catch (error) {
        console.error("Error fetching artists:", error);
      }
    };

    fetchArtists();
  },[refreshKey] );

  const fetchSpotifyArtists = async (query) => {
    try {
      const artists = await searchArtist(query);
      console.log("artists:", artists);
      setFilteredItems(artists);
      console.log("Filtered items:", artists);
    } catch (error) {
      console.error("Error fetching Spotify artistssssss:", error);
      setFilteredItems([]);
    }
  };

  const [formData, setFormData] = useState({
    name: "",
    photo: "",
    location: "",
    bio: "",
    genre: " ",
    platforms: "",
    age: "",
    spotifyId: "",
  });

  useEffect(() => {
    if (searchTerm) {
      fetchSpotifyArtists(searchTerm);
    } else {
      setFilteredItems([]);
    }
  }, [searchTerm]);

  const handleSelectArtist = async (artist) => {
    console.log("Artist before destrufture:", artist);
    const { name, photo, spotifyId } = artist;

    const artistImage = artist.image || "path/to/default/image.jpg";
    const artistGenres = "Unknown genre";

    const newFormData = {
      name,
      photo: artistImage,
      location: "",
      bio: "",
      genre: artistGenres,
      platforms: "",
      age: "",
      spotifyId: spotifyId,
    };

    setFormData(newFormData);
    console.log("Setting form data:", { name, photo: artistImage });
    setDropdownVisible(false);
    setModalIsOpen(true);
  };

  const openModal = () => {
    setModalIsOpen(true);
  };

  const closeModal = () => {
    setModalIsOpen(false);
  };

  const addArtist = (newArtist) => {
    setArtists((prevArtists) => [...prevArtists, newArtist]);
     console.log("refreshkey:",refreshKey);
    closeModal();
  };

  const deleteArtist = (name) => {
    setArtists((prevArtists) =>
      prevArtists.filter((artist) => artist.name !== name)
     
    );
  };

  return (
    <Layout>
      <div className="flex flex-col w-full h-[500] overflow-y-auto bg-customBlue mt-12">
        <div className="p-4 bg-customBlue text-white overflow-hidden">
          <div className="flex flex-row gap-12">
            <h1
              className="text-2xl font-semibold text-white ml-10 mb-4"
              style={{ fontFamily: "Poppins" }}
            >
              Artists
            </h1>

            <div className="flex items-center space-x-4">
              <div className="relative flex flex-row">
                <input
                  type="text"
                  placeholder="Search for..."
                  value={searchTerm}
                  onChange={(e) => setSearchTerm(e.target.value)}
                  onFocus={() => setDropdownVisible(true)}
                  className="p-2 pl-10 rounded-md border bg-customColor w-[260.75px] h-[46px] text-white mb-4"
                  style={{
                    borderRadius: "4px",
                    border: "0.6px solid  #343B4F",
                  }}
                />
                {/* <img
                  src="./assests/Search.png"
                  alt="Search Icon"
                  className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5"
                /> */}
              </div>
            </div>
            <button
              onClick={openModal}
              className="w-[105px] h-[44px] bg-customBlue text-white mt-2 border-white rounded p-2 flex justify-center items-center"
              style={{
                border: "1px solid #ffffff",
                position: "relative",
                left: "630px",
              }}
            >
              <span className="font-poppins" style={{ whiteSpace: "nowrap" }}>
                + Add Artist
              </span>
            </button>
          </div>

          {dropdownVisible && searchTerm && (
            <Dropdown items={filteredItems} onSelect={handleSelectArtist} />
          )}

          <ReviewFeature data={artists} onDelete={deleteArtist} />

          <ArtistModal
            isOpen={modalIsOpen}
            onRequestClose={closeModal}
            onSave={(newArtist) => {
              addArtist(newArtist);
              setRefreshKey((oldKey) => oldKey + 1);
            }}
            preFilledData={{
              name: formData.name,
              photo: formData.photo,
              spotifyId: formData.spotifyId,
            }}
          />
        </div>
      </div>
    </Layout>
  );
};

export default ReviewDataTable;

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