fix: stabilize dashboard crud action callbacks
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import type { ReactNode } from "react"
|
import type { ReactNode } from "react"
|
||||||
import { useCallback, useEffect, useState } from "react"
|
import { useCallback, useEffect, useRef, useState } from "react"
|
||||||
import {
|
import {
|
||||||
closestCenter,
|
closestCenter,
|
||||||
DndContext,
|
DndContext,
|
||||||
@@ -264,6 +264,12 @@ export function DashboardCrudPage<TItem, TPayload>({
|
|||||||
setEditingItem(null)
|
setEditingItem(null)
|
||||||
setDialogOpen(true)
|
setDialogOpen(true)
|
||||||
}, [onCreateItem])
|
}, [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) {
|
function openEditDialog(item: TItem) {
|
||||||
setEditingItem(item)
|
setEditingItem(item)
|
||||||
@@ -272,11 +278,11 @@ export function DashboardCrudPage<TItem, TPayload>({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onActionStateChange?.({
|
onActionStateChange?.({
|
||||||
onRefresh: () => void loadData(),
|
onRefresh: actionRefresh,
|
||||||
onCreate: openCreateDialog,
|
onCreate: actionCreate,
|
||||||
loading,
|
loading,
|
||||||
})
|
})
|
||||||
}, [loadData, loading, onActionStateChange, openCreateDialog])
|
}, [actionCreate, actionRefresh, loading, onActionStateChange])
|
||||||
|
|
||||||
function handleDialogOpenChange(open: boolean) {
|
function handleDialogOpenChange(open: boolean) {
|
||||||
if (saving) return
|
if (saving) return
|
||||||
|
|||||||
Reference in New Issue
Block a user