This issue involves React's hydration mechanism and custom elements, requiring deep React internals knowledge.
The issue describes a bug where React does not attach event listeners for custom element events during hydration, only after the first client render. This affects SSR frameworks like Next.js. The fix likely requires changes to React's hydration logic, which is complex and requires deep understanding of React internals.
When hydrating server-rendered markup, React does not attach event listeners for custom element events that have been configured on the custom element using the React's on<custom event name> prop syntax. The event handler is only attached after the first client render, not during hydration. This issue affects all React SSR frameworks like Next.js (linking a related issue from Next.js: https://github.com/vercel/next.js/issues/84091 - note that the issue is on React side).
React version: 19.2.3
Link to code example: https://codesandbox.io/p/devbox/thirsty-leaf-4n6r2t
The CodeSandbox simulates SSR + hydration using only React:
renderToStringhydrateRoot is used to hydrate the contentA custom element <my-element>:
Each React component (NotWorking & WorksWithForcedRerender) renders:
<my-element
onmy-event={() => {
console.log(
"React custom event handler fired from <component name> component"
);
alert("React custom event handler fired from <component name> component");
}}
/>
The onmy-event prop on <my-element> in <NotWorking> React component does not call the configured callback function and does not cause a native alert to pop up because React does properly attach event listener for the onmy-event prop during hydration.
However, the onmy-event prop on <my-element> in <WorksWithForcedRerender> React component properl
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!