Skip to content

fix: suppress hydration warning #6910

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

Merged
merged 4 commits into from
Feb 7, 2025
Merged
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
15 changes: 10 additions & 5 deletions packages/main/src/internal/withWebComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
const Component = (tagNameSuffix ? `${tagName}-${tagNameSuffix}` : tagName) as unknown as ComponentType<
CommonProps & { class?: string; ref?: Ref<RefType> }
>;

const [isDefined, setIsDefined] = useState(definedWebComponents.has(Component));

// regular props (no booleans, no slots and no events)
const regularProps = regularProperties.reduce((acc, name) => {
if (Object.prototype.hasOwnProperty.call(rest, name) && isPrimitiveAttribute(rest[name])) {
Expand Down Expand Up @@ -152,6 +150,9 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
return events;
}, {});

// In React 19 events aren't correctly attached after hydration
const [attachEvents, setAttachEvents] = useState(!webComponentsSupported || !Object.keys(eventHandlers).length); // apply workaround only for React19 and if event props are defined

// non web component related props, just pass them
const nonWebComponentRelatedProps = Object.entries(rest)
.filter(([key]) => !regularProperties.includes(key))
Expand Down Expand Up @@ -188,7 +189,11 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
});
}, [Component, ...propsToApply]);

if ((waitForDefine && !isDefined) || typeof window === 'undefined') {
useEffect(() => {
setAttachEvents(true);
}, []);

if (waitForDefine && !isDefined) {
return null;
}

Expand All @@ -203,7 +208,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
ref={componentRef}
{...booleanProps}
{...restRegularProps}
{...eventHandlers}
{...(attachEvents ? eventHandlers : {})}
{...nonWebComponentRelatedProps}
overflow-mode={overflowMode ?? (showOverflowInPopover ? 'Popover' : 'InPlace')}
// @ts-expect-error: text is available
Expand All @@ -222,7 +227,7 @@ export const withWebComponent = <Props extends Record<string, any>, RefType = Ui
ref={componentRef}
{...booleanProps}
{...regularProps}
{...eventHandlers}
{...(attachEvents ? eventHandlers : {})}
{...nonWebComponentRelatedProps}
class={className}
suppressHydrationWarning
Expand Down
Loading