-
Notifications
You must be signed in to change notification settings - Fork 67
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
React 18 Support: Cleanup DOM during hydration to avoid mismatch #341
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import ReactDOM from "react-dom/client" | ||
import { App } from "./app" | ||
import { SSRStyleID, mediaStyle } from "./Media" | ||
|
||
let root | ||
|
||
if (document.getElementById(SSRStyleID)) { | ||
// rehydration | ||
ReactDOM.hydrate(<App />, document.getElementById("react-root")) | ||
root = ReactDOM.hydrateRoot(document.getElementById("react-root"), <App />) | ||
} else { | ||
// client-side only | ||
const style = document.createElement("style") | ||
style.type = "text/css" | ||
style.id = SSRStyleID | ||
style.innerText = mediaStyle | ||
document.getElementsByTagName("head")[0].appendChild(style) | ||
ReactDOM.render(<App />, document.getElementById("react-root")) | ||
|
||
root = ReactDOM.createRoot(document.getElementById("react-root")) | ||
root.render(<App />) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
createClassName, | ||
castBreakpointsToIntegers, | ||
memoize, | ||
useIsFirstRender, | ||
} from "./Utils" | ||
import { BreakpointConstraint } from "./Breakpoints" | ||
|
||
|
@@ -340,10 +341,15 @@ export function createMedia< | |
>({}) | ||
MediaContext.displayName = "Media.Context" | ||
|
||
const MediaParentContext = React.createContext<{ | ||
type MediaParentContextValue = { | ||
hasParentMedia: boolean | ||
breakpointProps: MediaBreakpointProps<BreakpointKey> | ||
}>({ hasParentMedia: false, breakpointProps: {} }) | ||
} | ||
|
||
const MediaParentContext = React.createContext<MediaParentContextValue>({ | ||
hasParentMedia: false, | ||
breakpointProps: {}, | ||
}) | ||
MediaContext.displayName = "MediaParent.Context" | ||
|
||
const getMediaContextValue = memoize(onlyMatch => ({ | ||
|
@@ -394,128 +400,117 @@ export function createMedia< | |
} | ||
} | ||
|
||
const Media = class extends React.Component< | ||
MediaProps<BreakpointKey, Interaction> | ||
> { | ||
constructor(props) { | ||
super(props) | ||
validateProps(props) | ||
} | ||
const Media = (props: MediaProps<BreakpointKey, Interaction>) => { | ||
validateProps(props) | ||
|
||
const { | ||
children, | ||
className: passedClassName, | ||
style, | ||
interaction, | ||
...breakpointProps | ||
} = props | ||
|
||
const getMediaParentContextValue = React.useMemo(() => { | ||
return memoize( | ||
(newBreakpointProps: MediaBreakpointProps<BreakpointKey>) => ({ | ||
hasParentMedia: true, | ||
breakpointProps: newBreakpointProps, | ||
}) | ||
) | ||
}, []) | ||
|
||
static defaultProps = { | ||
className: "", | ||
style: {}, | ||
} | ||
const mediaParentContext = React.useContext(MediaParentContext) | ||
const childMediaParentContext = getMediaParentContextValue(breakpointProps) | ||
const { onlyMatch } = React.useContext(MediaContext) | ||
|
||
static contextType = MediaParentContext | ||
const id = React.useId() | ||
const isClient = typeof window !== "undefined" | ||
const isFirstRender = useIsFirstRender() | ||
|
||
getMediaParentContextValue = memoize( | ||
(breakpointProps: MediaBreakpointProps<BreakpointKey>) => ({ | ||
hasParentMedia: true, | ||
breakpointProps, | ||
}) | ||
) | ||
let className: string | null | ||
if (props.interaction) { | ||
className = createClassName("interaction", props.interaction) | ||
} else { | ||
if (props.at) { | ||
const largestBreakpoint = mediaQueries.breakpoints.largestBreakpoint | ||
if (props.at === largestBreakpoint) { | ||
console.warn( | ||
"[@artsy/fresnel] " + | ||
"`at` is being used with the largest breakpoint. " + | ||
"Consider using `<Media greaterThanOrEqual=" + | ||
`"${largestBreakpoint}">\` to account for future ` + | ||
`breakpoint definitions outside of this range.` | ||
) | ||
} | ||
} | ||
|
||
const type = propKey(breakpointProps) | ||
const breakpoint = breakpointProps[type]! | ||
className = createClassName(type, breakpoint) | ||
} | ||
|
||
render() { | ||
const props = this.props | ||
const { | ||
children, | ||
className: passedClassName, | ||
style, | ||
interaction, | ||
...breakpointProps | ||
} = props | ||
const mediaParentContextValue = this.getMediaParentContextValue( | ||
breakpointProps | ||
) | ||
const doesMatchParent = | ||
!mediaParentContext.hasParentMedia || | ||
intersection( | ||
mediaQueries.breakpoints.toVisibleAtBreakpointSet( | ||
mediaParentContext.breakpointProps | ||
), | ||
mediaQueries.breakpoints.toVisibleAtBreakpointSet(breakpointProps) | ||
).length > 0 | ||
|
||
const renderChildren = | ||
doesMatchParent && | ||
(onlyMatch === undefined || | ||
mediaQueries.shouldRenderMediaQuery( | ||
{ ...breakpointProps, interaction }, | ||
onlyMatch | ||
)) | ||
|
||
// Append a unique id to the className (consistent on server and client) | ||
const uniqueComponentId = ` fresnel-${id}` | ||
className += uniqueComponentId | ||
|
||
/** | ||
* SPECIAL CASE: | ||
* If we're on the client, this is the first render, and we are not going | ||
* to render the children, we need to cleanup the the server-rendered HTML | ||
* to avoid a hydration mismatch on React 18+. We do this by grabbing the | ||
* already-existing element(s) directly from the DOM using the unique class | ||
* id and clearing its contents. This solution follows one of the | ||
* suggestions from Dan Abromov here: | ||
* | ||
* https://github.com/facebook/react/issues/23381#issuecomment-1096899474 | ||
* | ||
* This will not have a negative impact on client-only rendering because | ||
* either 1) isFirstRender will be false OR 2) the element won't exist yet | ||
* so there will be nothing to clean up. It will only apply on SSR'd HTML | ||
* on initial hydration. | ||
*/ | ||
if (isClient && isFirstRender && !renderChildren) { | ||
const containerEls = document.getElementsByClassName(uniqueComponentId) | ||
Array.from(containerEls).forEach(el => (el.innerHTML = "")) | ||
} | ||
|
||
return ( | ||
<MediaParentContext.Consumer> | ||
{mediaParentContext => { | ||
return ( | ||
<MediaParentContext.Provider value={childMediaParentContext}> | ||
{(() => { | ||
if (props.children instanceof Function) { | ||
return props.children(className, renderChildren) | ||
} else { | ||
return ( | ||
<MediaParentContext.Provider value={mediaParentContextValue}> | ||
<MediaContext.Consumer> | ||
{({ onlyMatch } = {}) => { | ||
let className: string | null | ||
if (props.interaction) { | ||
className = createClassName( | ||
"interaction", | ||
props.interaction | ||
) | ||
} else { | ||
if (props.at) { | ||
const largestBreakpoint = | ||
mediaQueries.breakpoints.largestBreakpoint | ||
if (props.at === largestBreakpoint) { | ||
// TODO: We should look into making React’s __DEV__ available | ||
// and have webpack completely compile these away. | ||
let ownerName = null | ||
try { | ||
const owner = (this as any)._reactInternalFiber | ||
._debugOwner.type | ||
ownerName = owner.displayName || owner.name | ||
} catch (err) { | ||
// no-op | ||
} | ||
|
||
console.warn( | ||
"[@artsy/fresnel] " + | ||
"`at` is being used with the largest breakpoint. " + | ||
"Consider using `<Media greaterThanOrEqual=" + | ||
`"${largestBreakpoint}">\` to account for future ` + | ||
`breakpoint definitions outside of this range.${ | ||
ownerName | ||
? ` It is being used in the ${ownerName} component.` | ||
: "" | ||
}` | ||
) | ||
} | ||
} | ||
|
||
const type = propKey(breakpointProps) | ||
const breakpoint = breakpointProps[type]! | ||
className = createClassName(type, breakpoint) | ||
} | ||
|
||
const doesMatchParent = | ||
!mediaParentContext.hasParentMedia || | ||
intersection( | ||
mediaQueries.breakpoints.toVisibleAtBreakpointSet( | ||
mediaParentContext.breakpointProps | ||
), | ||
mediaQueries.breakpoints.toVisibleAtBreakpointSet( | ||
breakpointProps | ||
) | ||
).length > 0 | ||
const renderChildren = | ||
doesMatchParent && | ||
(onlyMatch === undefined || | ||
mediaQueries.shouldRenderMediaQuery( | ||
{ ...breakpointProps, interaction }, | ||
onlyMatch | ||
)) | ||
|
||
if (props.children instanceof Function) { | ||
return props.children(className, renderChildren) | ||
} else { | ||
return ( | ||
<div | ||
className={`fresnel-container ${className} ${passedClassName}`} | ||
style={style} | ||
suppressHydrationWarning={!renderChildren} | ||
> | ||
{renderChildren ? props.children : null} | ||
</div> | ||
) | ||
} | ||
}} | ||
</MediaContext.Consumer> | ||
</MediaParentContext.Provider> | ||
<div | ||
className={`fresnel-container ${className} ${passedClassName}`} | ||
style={style} | ||
suppressHydrationWarning={!renderChildren} | ||
> | ||
{renderChildren ? props.children : null} | ||
</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 Much less code |
||
) | ||
}} | ||
</MediaParentContext.Consumer> | ||
) | ||
} | ||
} | ||
})()} | ||
</MediaParentContext.Provider> | ||
) | ||
} | ||
|
||
return { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple, easy to understand javascript saves the day again. It is almost ironic.