React TypeScript: Delete with Confirmation Modal and RTK Query
In this example, we will implement an Agency DataTable with a delete functionality using React, TypeScript, RTK Query, and React Toastify.
Features
import { useState } from "react";
import { toast } from "react-toastify";
import {
useGetAllAgencyQuery,
useDeleteAgencyMutation,
} from "../services/agencyApi";
const AgencyTable = () => {
// Store the agency ID selected for deletion
const [selectedAgencyId, setSelectedAgencyId] = useState<number | null>(
null
);
// Get all agencies
const {
data: agencyData,
isLoading: isAgencyLoading,
isError: isAgencyError,
} = useGetAllAgencyQuery();
// Delete agency mutation
const [deleteAgency, { isLoading: isDeleting }] =
useDeleteAgencyMutation();
// Open confirmation modal
const handleDeleteClick = (agencyId: number) => {
setSelectedAgencyId(agencyId);
};
// Close confirmation modal
const ha
Discussion
Begin the discussion
Begin something meaningful by sharing your ideas.