Skip to content

Commit a718761

Browse files
committed
fix: Hydration mismatch on table collection
1 parent 932f5f5 commit a718761

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

packages/react-notion-x/src/styles.css

-1
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,6 @@ svg.notion-page-icon {
12941294
float: left;
12951295
min-width: var(--notion-max-width);
12961296
padding-left: 0;
1297-
transition: padding 200ms ease-out;
12981297
}
12991298

13001299
.notion-table-header {

packages/react-notion-x/src/third-party/collection-view-table.tsx

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import type * as React from 'react'
22

33
import { useNotionContext } from '../context'
44
import { type CollectionViewProps } from '../types'
@@ -7,6 +7,7 @@ import { CollectionColumnTitle } from './collection-column-title'
77
import { CollectionGroup } from './collection-group'
88
import { getCollectionGroups } from './collection-utils'
99
import { Property } from './property'
10+
import { useClientStyle } from './react-use'
1011

1112
const defaultBlockIds: string[] = []
1213

@@ -72,21 +73,18 @@ function Table({
7273
} & Omit<CollectionViewProps, 'collectionData'>) {
7374
const { recordMap, linkTableTitleProperties } = useNotionContext()
7475

75-
const tableStyle = React.useMemo(
76-
() => ({
76+
const tableStyle = useClientStyle(
77+
{
7778
width,
7879
maxWidth: width
79-
}),
80-
[width]
80+
},
81+
{ visibility: 'hidden' }
8182
)
8283

83-
const tableViewStyle = React.useMemo(
84-
() => ({
85-
paddingLeft: padding,
86-
paddingRight: padding
87-
}),
88-
[padding]
89-
)
84+
const tableViewStyle = useClientStyle({
85+
paddingLeft: padding,
86+
paddingRight: padding
87+
})
9088

9189
let properties = []
9290

packages/react-notion-x/src/third-party/react-use.ts

+16
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,19 @@ export const useLocalStorage = <T>(
211211

212212
return [state, set, remove]
213213
}
214+
215+
// Style mismatches between server rendering and client hydration can act
216+
// unpredictably. Style that depends on the client state should use this hook
217+
// can prevent the unpredictable behavior. More details here:
218+
// https://github.com/vercel/next.js/issues/17463
219+
export const useClientStyle = (
220+
clientStyle: React.CSSProperties,
221+
serverStyle: React.CSSProperties = {}
222+
) => {
223+
const [isMounted, setIsMounted] = useState(false)
224+
useEffect(() => {
225+
setIsMounted(true)
226+
}, [])
227+
228+
return isMounted ? clientStyle : serverStyle
229+
}

0 commit comments

Comments
 (0)