fix: stabilize dashboard crud action callbacks

This commit is contained in:
mlogclub
2026-06-22 12:49:59 +08:00
parent f0eb694803
commit eb7da6ef80
@@ -1,7 +1,7 @@
"use client"
import type { ReactNode } from "react"
import { useCallback, useEffect, useState } from "react"
import { useCallback, useEffect, useRef, useState } from "react"
import {
closestCenter,
DndContext,
@@ -264,6 +264,12 @@ export function DashboardCrudPage<TItem, TPayload>({
setEditingItem(null)
setDialogOpen(true)
}, [onCreateItem])
const loadDataRef = useRef(loadData)
const openCreateDialogRef = useRef(openCreateDialog)
loadDataRef.current = loadData
openCreateDialogRef.current = openCreateDialog
const actionRefresh = useCallback(() => void loadDataRef.current(), [])
const actionCreate = useCallback(() => openCreateDialogRef.current(), [])
function openEditDialog(item: TItem) {
setEditingItem(item)
@@ -272,11 +278,11 @@ export function DashboardCrudPage<TItem, TPayload>({
useEffect(() => {
onActionStateChange?.({
onRefresh: () => void loadData(),
onCreate: openCreateDialog,
onRefresh: actionRefresh,
onCreate: actionCreate,
loading,
})
}, [loadData, loading, onActionStateChange, openCreateDialog])
}, [actionCreate, actionRefresh, loading, onActionStateChange])
function handleDialogOpenChange(open: boolean) {
if (saving) return