Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(collection): two collection hydration errors #607

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/react-notion-x/src/third-party/collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ function CollectionViewBlock({
[collectionState, setCollectionState]
)

let { width: windowWidth } = useWindowSize()
if (isServer) {
windowWidth = 1024
}
const { width: windowWidth } = useWindowSize()

const collection = recordMap.collection[collectionId]?.value
const collectionView = recordMap.collection_view[collectionViewId]?.value
Expand Down Expand Up @@ -152,7 +149,7 @@ function CollectionViewBlock({
}

const padding =
isServer && !isMounted ? 96 : Math.trunc((width - notionBodyWidth) / 2)
isServer || !isMounted ? 96 : Math.trunc((width - notionBodyWidth) / 2)
style.paddingLeft = padding
style.paddingRight = padding

Expand All @@ -161,7 +158,12 @@ function CollectionViewBlock({
width,
padding
}
}, [windowWidth, parentPage, collectionView?.type, isMounted])
}, [
collectionView?.type,
windowWidth,
parentPage?.format?.page_full_width,
isMounted
])

// console.log({
// width,
Expand Down
24 changes: 11 additions & 13 deletions packages/react-notion-x/src/third-party/react-use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,32 @@ function off<T extends Window | Document | HTMLElement | EventTarget>(

const isBrowser = typeof window !== 'undefined'

export const useWindowSize = (
initialWidth = Infinity,
initialHeight = Infinity
) => {
const [state, setState] = useRafState<{ width: number; height: number }>({
width: isBrowser ? window.innerWidth : initialWidth,
height: isBrowser ? window.innerHeight : initialHeight
export const useWindowSize = (initialWidth = 1024, initialHeight = 768) => {
const [dimensions, setDimensions] = useRafState<{
width: number
height: number
}>({
width: initialWidth,
height: initialHeight
})

useEffect((): (() => void) | void => {
if (isBrowser) {
const handler = () => {
setState({
setDimensions({
width: window.innerWidth,
height: window.innerHeight
})
}

on(window, 'resize', handler)

return () => {
off(window, 'resize', handler)
}
handler()
return () => off(window, 'resize', handler)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return state
return dimensions
}

export const useEffectOnce = (effect: EffectCallback) => {
Expand Down
Loading