Skip to content

Commit f5f0ac4

Browse files
committed
fix(table): fix hydration error in table component
1 parent f9ba814 commit f5f0ac4

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/app/(private)/dashboard/categories/client.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { useDisclosure, addToast } from "@heroui/react";
66
import { useForm } from "react-hook-form";
77
import { zodResolver } from "@hookform/resolvers/zod";
88
import { SquarePenIcon, Trash2 } from "lucide-react";
9-
109
import {
1110
createCategory,
1211
deleteCategory,
@@ -15,7 +14,6 @@ import {
1514
import { CategoryDrawer } from "@/components/categories";
1615
import { DeleteModal, HeadingDashboard, Table } from "@/components/ui";
1716
import { categorySchema } from "@/lib/zod";
18-
1917
import type { Category, CategoryForm, TableAction, TableColumn } from "@/types";
2018

2119
type Props = {
@@ -24,17 +22,21 @@ type Props = {
2422

2523
export default function CategoriesPageClient({ categories }: Props) {
2624
const router = useRouter();
25+
26+
// States
2727
const [editingCategory, setEditingCategory] = useState<Category | null>(null);
2828
const [categoryToDelete, setCategoryToDelete] = useState<string | null>(null);
2929
const [loading, setLoading] = useState<boolean>(false);
3030

31+
// Hooks
3132
const { isOpen, onOpen, onClose } = useDisclosure();
3233
const {
3334
isOpen: isConfirmOpen,
3435
onOpen: onConfirmOpen,
3536
onClose: onConfirmClose,
3637
} = useDisclosure();
3738

39+
// Form
3840
const {
3941
register,
4042
handleSubmit,
@@ -45,6 +47,7 @@ export default function CategoriesPageClient({ categories }: Props) {
4547
defaultValues: { name: "" },
4648
});
4749

50+
// Functions
4851
const openEditModal = (category: Category) => {
4952
setEditingCategory(category);
5053
reset({ name: category.name });
@@ -100,6 +103,7 @@ export default function CategoriesPageClient({ categories }: Props) {
100103
}
101104
};
102105

106+
// Constants
103107
const columns: TableColumn[] = [
104108
{ name: "Name", uid: "name", sortable: true },
105109
{ name: "Slug", uid: "slug", sortable: true },

src/components/ui/table.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use client";
22

33
import { Key, useCallback, useEffect, useMemo, useRef, useState } from "react";
4+
import dynamic from "next/dynamic";
45
import {
5-
Table as HeroUITable,
66
TableColumn as HeroUITableColumn,
77
TableHeader,
88
TableBody,
@@ -22,14 +22,20 @@ import {
2222
} from "@heroui/react";
2323
import { ChevronDown, EllipsisVertical, Plus, Search } from "lucide-react";
2424
import { capitalize } from "@/lib/utils";
25-
2625
import type {
2726
CellRendererProps,
2827
FilterOption,
2928
TableAction,
3029
TableColumn,
3130
} from "@/types";
3231

32+
const HeroUITable = dynamic(
33+
() => import("@heroui/table").then((c) => c.Table),
34+
{
35+
ssr: false,
36+
},
37+
);
38+
3339
type Props = {
3440
data: any[];
3541
columns: TableColumn[];
@@ -99,6 +105,7 @@ export default function Table({
99105
maxHeight = "600px",
100106
emptyContent = "No data found",
101107
}: Props) {
108+
// States
102109
const [filterValue, setFilterValue] = useState<string>("");
103110
const [selectedKeys, setSelectedKeys] = useState<Selection>(new Set([]));
104111
const [visibleColumns, setVisibleColumns] = useState<Selection>(() => {
@@ -123,7 +130,7 @@ export default function Table({
123130

124131
// Visible columns filter (server-side)
125132
const headerColumns = useMemo(() => {
126-
let cols = [...columns];
133+
const cols = [...columns];
127134

128135
if (actions.length > 0) {
129136
cols.push({ name: "ACTIONS", uid: "actions", align: "center" });
@@ -153,7 +160,6 @@ export default function Table({
153160
);
154161
}
155162

156-
// Filter by status/category
157163
if (
158164
filterKey &&
159165
statusFilter !== "all" &&
@@ -365,7 +371,7 @@ export default function Table({
365371
[serverSide, onFilterChange],
366372
);
367373

368-
// Timeout cleanup
374+
// Effects
369375
useEffect(() => {
370376
return () => {
371377
if (searchTimeoutRef.current) {
@@ -621,9 +627,9 @@ export default function Table({
621627
)}
622628
</TableHeader>
623629
<TableBody
624-
emptyContent={emptyContent}
625630
items={sortedItems}
626631
isLoading={loading}
632+
emptyContent={emptyContent}
627633
loadingContent="Loading data..."
628634
>
629635
{(item) => (

0 commit comments

Comments
 (0)