From a58814dec903b63d15c8b19ac9999fe538267176 Mon Sep 17 00:00:00 2001 From: flavien Date: Thu, 17 Oct 2024 11:01:35 +0200 Subject: [PATCH] [TreeView] Fix hydration error --- .../internals/corePlugins/useTreeViewId/useTreeViewId.ts | 8 ++++---- .../corePlugins/useTreeViewId/useTreeViewId.types.ts | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.ts b/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.ts index cbf443431f4ec..eac7a5bd705f7 100644 --- a/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.ts +++ b/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.ts @@ -8,11 +8,9 @@ export const useTreeViewId: TreeViewPlugin = ({ state, setState, }) => { - const treeId = state.id.treeId; - React.useEffect(() => { setState((prevState) => { - if (prevState.id.treeId === params.id) { + if (params.id === prevState.id.providedTreeId && prevState.id.treeId !== undefined) { return prevState; } @@ -23,6 +21,8 @@ export const useTreeViewId: TreeViewPlugin = ({ }); }, [setState, params.id]); + const treeId = state.id.treeId ?? state.id.providedTreeId; + return { getRootProps: () => ({ id: treeId, @@ -37,4 +37,4 @@ useTreeViewId.params = { id: true, }; -useTreeViewId.getInitialState = ({ id }) => ({ id: { treeId: id ?? createTreeViewDefaultId() } }); +useTreeViewId.getInitialState = ({ id }) => ({ id: { treeId: undefined, providedTreeId: id } }); diff --git a/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.types.ts b/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.types.ts index 392a77e8585cc..6704907447de9 100644 --- a/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.types.ts +++ b/packages/x-tree-view/src/internals/corePlugins/useTreeViewId/useTreeViewId.types.ts @@ -12,7 +12,8 @@ export type UseTreeViewIdDefaultizedParameters = UseTreeViewIdParameters; export interface UseTreeViewIdState { id: { - treeId: string; + treeId: string | undefined; + providedTreeId: string | undefined; }; }