You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[labs/nextjs] reflect: true on props are throwing error #3858
When props are reflected in the component, like count: { type: Number, reflect: true }, I'm getting following error in the dev tools console: extra attributes from the server.
It looks like Next.js is having hard times running hydration because server template and client templet don't match.
There are many cases where we need to reflect props, is there a way to fix this somehow?
Per offline convo with @justinfagnani this is something we could have bespoke handling in the @lit-labs/react wrapper where we're able to check property options and include it to the host element on hydrate/initial render, but that won't exactly help with using bare Lit components like this, so I'm still leaning towards suppressing hydration warning.
Hi, just a suggestion, not even sure if that can be achieved ...
Would it be possible to add props that have reflect: true and simple default value set in constructor (strings, numbers, booleans) as part of template render on the server?
The hydration warnings are specifically happening when the server add attributes that weren't included in the original template as props. Having defaults on the server won't prevent that.
To prevent this hydration warning, we need to either not reflect attributes on the server (I don't think we should do this), or find a way to include the added attribute as props at React hydration time.
For users of the nextjs plugin or the labs/ssr-react package, I think we might include logic for this on the client jsx functions or patch createElement. Right now, our client versions of jsx and createElement simply re-export the original React ones, but we could patch those like we do for the server environment.
I do wonder if there's benefit to adding all of this extra logic instead of just adding suppressHydrationWarning={true}.
Thanks for coming back.
I don't think that suppressHydrationWarning={true} is way forward either. On Next.js page I see that there is a pitfall using this solution: This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections..
For now, we are passing all reflected props that have a default value set in constructor, something along those lines:
in our component class we have:
export class McButton extends LitElement {
@property({ type: String, reflect: true })
public fit: Fit = 'medium';
}
and then when we use component: <mc-button fit="medium">Button<mc-button>
This seems to solve template mismatch errors, but of course is not ideal. In some cases we also set some extra aria attributes in the firstUpdated life cycle function - this is also causing template mismatches.
@augustjk the logic to bind reflected properties as attributes could be added both to the React wrapper and to the createElement patch... not sure if it's a good idea, but it's possible.
So the logic would be to patch createElement (and runtime JSX functions) so that if the tag provided is a Lit component, look at the Lit component class, find all properties with reflect: true, if there are no user provided prop for those create an instance to grab default values for them, and provide them to React as props, any user provided prop will override this.
This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections.
is mentioned specifically with regards to using a state to do a 2-pass render on the client to explicitly show different content on the client versus what the server rendered.
React's note about the suppressHydrationWarning flag states
This only works one level deep, and is intended to be an escape hatch. Don’t overuse it. Unless it’s text content, React still won’t attempt to patch it up, so it may remain inconsistent until future updates.
In our case the warnings are about mismatching attributes that don't actually change the content of the component from what was rendered on the server versus the client. The nature of reflecting properties to attributes is that the discrepancy is expected and therefore okay to suppress.
As we're talking about attributes and not text content, React "won't attempt to patch it" so there's actually no extra work happening during hydration due to this mismatch.
Lastly, we're considering the best fix for #3418 to be adding defer-hydration attribute to the host element so that we can imperatively add initial properties to the element before triggering the element's initial update on the client. The addition of the defer-hydration can trigger the same warning during React's hydration which we still consider to be innocuous and should suppress.
An interesting observation I had is even without using the @lit-labs/nextjs plugin, using Lit components with attribute reflecting properties emits the same warning because the element already contains the reflected attribute from the custom element upgrading by the time React tries to hydrate, even though the HTML from the server never contained the attribute.
Discussed this case a bit more during our eng meeting just now. Since the suppressHydrationWarning prop needs to be set on the client side, we can easily do this for components using @lit-labs/react which we're planning to add with #3885.
But for bare custom elements, we'd need to also patch client-side createElement or jsx-runtime to add the prop, which currently are just passthroughs of the React ones.
Or we need to do extra logic during SSR to not reflect attributes if they come from default prop which might be hard to do. (This could become much simpler to do with new decorators but that'll still take some time)
For the time being we're looking to not do anything for this case for bare custom elements. The hydration warnings only happen during dev mode and could be ignored. Or, users would have to add the default value in as JSX prop, or add the suppressHydrationWarning themselves to the custom element to prevent the warning.
We'll definitely revisit this when the better custom element support for React lands (maybe React 19?) as we'll likely need to touch client side element creation then.
@aneta-kjeldsen Sorry this ultimately means we're not solving your specific use case. Is getting rid of the hydration warning for bare custom elements a high priority for you?
For now, as an interim solution, we can just ask users to pass all reflected props with default values to our components - that's fine. For other attributes (like aria-labels) we will have to find another solution - maybe move the logic from firstUpdated to some other life cycle method.
I think it would be good to add an option to the plugin that will add suppressHydrationWarning prop to Lit components client side.
The option could even take an array of tag names to add the prop only to those tag names, or just a true for suppress for all registered custom elements, maybe..
changed the title [-][@lit-labs/nextjs] `reflect: true` on props are throwing error[/-][+][labs/nextjs] `reflect: true` on props are throwing error[/+]on Oct 16, 2023
Plugin option to suppress hydration for custom elements, and making it take a list of custom elements or a pattern match (like prefix) on the tag name, would be a potential stop gap for this.
The "proper" solution might involve reading the registered component class, and if it's a Lit element, try to read property option to see if it reflects, and try and get the default value and include that extra as a prop, but only do this for the very first time.
Unfortunately, while we can use this to detect the hydration pass, that still means creating a React component wrapper for the custom element, do the inspection for the reflecting attributes, and provide those to React as props to satisfy the hydration check. For bare custom elements in JSX, that means monkeypatching React.createElement or an alternate jsx-runtime for the client too.
Is there any update on this issue? I wanted to bring attention to it. We are developing a web component library and encountering a hydration error with Next.js 15 (React 19). As mentioned in the issue, we are getting a hydration error when default-valued properties try to reflect back into the DOM. Should we consider abandoning reflect: true in our components?
@sorvell is working on #4895 which will help in some of these cases - when the reflection is happening because of the initial value. Are the other cases - when reflection is happening because of the associated property being set - a problem too? Is it only a problem if the property and attribute name are different?
A good step forward here would be to add some failing test cases.
Activity
augustjk commentedon Apr 28, 2023
I've seen the "extra attributes from the server" errors while trying out solutions for #3418 as well. This might be unavoidable so it's worth considering suppressing this via https://react.dev/reference/react-dom/client/hydrateRoot#suppressing-unavoidable-hydration-mismatch-errors which we'll add to the host element during SSR.
augustjk commentedon Apr 28, 2023
Per offline convo with @justinfagnani this is something we could have bespoke handling in the
@lit-labs/reactwrapper where we're able to check property options and include it to the host element on hydrate/initial render, but that won't exactly help with using bare Lit components like this, so I'm still leaning towards suppressing hydration warning.aneta-kjeldsen commentedon May 1, 2023
Hi, just a suggestion, not even sure if that can be achieved ...
Would it be possible to add props that have
reflect: trueand simple default value set in constructor (strings, numbers, booleans) as part of template render on the server?augustjk commentedon May 1, 2023
The hydration warnings are specifically happening when the server add attributes that weren't included in the original template as props. Having defaults on the server won't prevent that.
To prevent this hydration warning, we need to either not reflect attributes on the server (I don't think we should do this), or find a way to include the added attribute as props at React hydration time.
For users of the nextjs plugin or the labs/ssr-react package, I think we might include logic for this on the client
jsxfunctions or patchcreateElement. Right now, our client versions ofjsxandcreateElementsimply re-export the original React ones, but we could patch those like we do for the server environment.I do wonder if there's benefit to adding all of this extra logic instead of just adding
suppressHydrationWarning={true}.aneta-kjeldsen commentedon May 2, 2023
Thanks for coming back.
I don't think that
suppressHydrationWarning={true}is way forward either. On Next.js page I see that there is a pitfall using this solution:This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections..For now, we are passing all reflected props that have a default value set in constructor, something along those lines:
in our component class we have:
and then when we use component:
<mc-button fit="medium">Button<mc-button>This seems to solve template mismatch errors, but of course is not ideal. In some cases we also set some extra aria attributes in the
firstUpdatedlife cycle function - this is also causing template mismatches.justinfagnani commentedon May 2, 2023
@augustjk the logic to bind reflected properties as attributes could be added both to the React wrapper and to the createElement patch... not sure if it's a good idea, but it's possible.
augustjk commentedon May 2, 2023
So the logic would be to patch
createElement(and runtime JSX functions) so that if the tag provided is a Lit component, look at the Lit component class, find all properties withreflect: true, if there are no user provided prop for those create an instance to grab default values for them, and provide them to React as props, any user provided prop will override this.We really only need to do this on the initial client render/hydration of the React component, but there isn't a flag that we can use for that. React docs suggest creating a separate state to track that https://react.dev/reference/react-dom/client/hydrateRoot#handling-different-client-and-server-content.
augustjk commentedon May 3, 2023
We've done a team huddle on this and we're going with adding
suppressHydrationWarningclient-side for these components.@aneta-kjeldsen your concern about the pitfall
is mentioned specifically with regards to using a state to do a 2-pass render on the client to explicitly show different content on the client versus what the server rendered.
React's note about the
suppressHydrationWarningflag statesIn our case the warnings are about mismatching attributes that don't actually change the content of the component from what was rendered on the server versus the client. The nature of reflecting properties to attributes is that the discrepancy is expected and therefore okay to suppress.
As we're talking about attributes and not text content, React "won't attempt to patch it" so there's actually no extra work happening during hydration due to this mismatch.
Lastly, we're considering the best fix for #3418 to be adding
defer-hydrationattribute to the host element so that we can imperatively add initial properties to the element before triggering the element's initial update on the client. The addition of thedefer-hydrationcan trigger the same warning during React's hydration which we still consider to be innocuous and should suppress.An interesting observation I had is even without using the
@lit-labs/nextjsplugin, using Lit components with attribute reflecting properties emits the same warning because the element already contains the reflected attribute from the custom element upgrading by the time React tries to hydrate, even though the HTML from the server never contained the attribute.aneta-kjeldsen commentedon May 3, 2023
Thanks for looking into it.
augustjk commentedon May 11, 2023
Discussed this case a bit more during our eng meeting just now. Since the
suppressHydrationWarningprop needs to be set on the client side, we can easily do this for components using@lit-labs/reactwhich we're planning to add with #3885.But for bare custom elements, we'd need to also patch client-side
createElementorjsx-runtimeto add the prop, which currently are just passthroughs of the React ones.Or we need to do extra logic during SSR to not reflect attributes if they come from default prop which might be hard to do. (This could become much simpler to do with new decorators but that'll still take some time)
For the time being we're looking to not do anything for this case for bare custom elements. The hydration warnings only happen during dev mode and could be ignored. Or, users would have to add the default value in as JSX prop, or add the
suppressHydrationWarningthemselves to the custom element to prevent the warning.We'll definitely revisit this when the better custom element support for React lands (maybe React 19?) as we'll likely need to touch client side element creation then.
@aneta-kjeldsen Sorry this ultimately means we're not solving your specific use case. Is getting rid of the hydration warning for bare custom elements a high priority for you?
aneta-kjeldsen commentedon May 12, 2023
For now, as an interim solution, we can just ask users to pass all reflected props with default values to our components - that's fine. For other attributes (like aria-labels) we will have to find another solution - maybe move the logic from
firstUpdatedto some other life cycle method.augustjk commentedon Oct 16, 2023
I think it would be good to add an option to the plugin that will add suppressHydrationWarning prop to Lit components client side.
The option could even take an array of tag names to add the prop only to those tag names, or just a
truefor suppress for all registered custom elements, maybe..[-][@lit-labs/nextjs] `reflect: true` on props are throwing error[/-][+][labs/nextjs] `reflect: true` on props are throwing error[/+]augustjk commentedon Oct 27, 2023
Discussed some more options with @michaelwarren1106 about this.
Plugin option to suppress hydration for custom elements, and making it take a list of custom elements or a pattern match (like prefix) on the tag name, would be a potential stop gap for this.
The "proper" solution might involve reading the registered component class, and if it's a Lit element, try to read property option to see if it reflects, and try and get the default value and include that extra as a prop, but only do this for the very first time.
augustjk commentedon Jan 5, 2024
useSyncExternalStoreapparently has a code path that'll run on the server and on hydration, which allows us to have some bespoke logic there. Source https://twitter.com/tkdodo/status/1741068994981826947 and credit to @ceIia for suggesting we might make use of this.Unfortunately, while we can use this to detect the hydration pass, that still means creating a React component wrapper for the custom element, do the inspection for the reflecting attributes, and provide those to React as props to satisfy the hydration check. For bare custom elements in JSX, that means monkeypatching
React.createElementor an alternatejsx-runtimefor the client too.sacitgonen commentedon Feb 12, 2025
Is there any update on this issue? I wanted to bring attention to it. We are developing a web component library and encountering a hydration error with Next.js 15 (React 19). As mentioned in the issue, we are getting a hydration error when default-valued properties try to reflect back into the DOM. Should we consider abandoning reflect: true in our components?
justinfagnani commentedon Feb 12, 2025
@sorvell is working on #4895 which will help in some of these cases - when the reflection is happening because of the initial value. Are the other cases - when reflection is happening because of the associated property being set - a problem too? Is it only a problem if the property and attribute name are different?
A good step forward here would be to add some failing test cases.