Skip to content

Commit

Permalink
fix: Hydration mismatch on table collection
Browse files Browse the repository at this point in the history
  • Loading branch information
alaycock committed Jan 13, 2025
1 parent 932f5f5 commit eaa18ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
22 changes: 10 additions & 12 deletions packages/react-notion-x/src/third-party/collection-view-table.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import type * as React from 'react'

import { useNotionContext } from '../context'
import { type CollectionViewProps } from '../types'
Expand All @@ -7,6 +7,7 @@ import { CollectionColumnTitle } from './collection-column-title'
import { CollectionGroup } from './collection-group'
import { getCollectionGroups } from './collection-utils'
import { Property } from './property'
import { useClientStyle } from './react-use'

const defaultBlockIds: string[] = []

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

const tableStyle = React.useMemo(
() => ({
const tableStyle = useClientStyle(
{
width,
maxWidth: width
}),
[width]
},
{ visibility: 'hidden' }
)

const tableViewStyle = React.useMemo(
() => ({
paddingLeft: padding,
paddingRight: padding
}),
[padding]
)
const tableViewStyle = useClientStyle({
paddingLeft: padding,
paddingRight: padding
})

let properties = []

Expand Down
16 changes: 16 additions & 0 deletions packages/react-notion-x/src/third-party/react-use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,19 @@ export const useLocalStorage = <T>(

return [state, set, remove]
}

// Style mismatches between server rendering and client hydration can act
// unpredictably. Style that depends on the client state should use this hook
// can prevent the unpredictable behavior. More details here:
// https://github.com/vercel/next.js/issues/17463
export const useClientStyle = (
clientStyle: React.CSSProperties,
serverStyle: React.CSSProperties = {}
) => {
const [isMounted, setIsMounted] = useState(false)
useEffect(() => {
setIsMounted(true)
}, [])

return isMounted ? clientStyle : serverStyle
}

0 comments on commit eaa18ce

Please sign in to comment.