I need to embed a Lit web component in a React application. The web component at some point will be dispatching a custom event, and I need to capture it in the React application and react accordingly.
However, for some reason I am failing to capture the event. I have very little to no experience using React, so this is making it more difficult.
so I have my <custom-web-component/>
embedded in the tsx file.
Then I tried to register an event listener like below:
React.useEffect(() => {
window.addEventListener("myCustomEvent", onCustomEvent);
// cleanup this component
return () => {
window.removeEventListener("myCustomEvent", onCustomEvent);
};
}, []);
function onCustomEvent(event: Event) {
const customEventDetail = (event as CustomEvent).detail as EventDetail;
console.log(customEventDetail);
}
However, when the event is triggered by the custom component, the event listener is not capturing it and hence I don’t get anything logged in the console.
2
Answers
The issue is that you listen and emit in different scopes. When you emit in a web component that does not "escape" the shadowDOM. You can do a couple of things, one is just putting an event handler on a component, which with this kind of component should work out of the box. You can listen for events on that component’s shadowDOM or change event emitter config to pass through shadomDOM and be "accessible" on window.
If you set
bubbles: true, composed: true
when emitting, you should be able to do what you want.But to be frank, with lit components. The easiest way is just to follow the documentation. What you want to achive should work out of the box as
<my-lit-component onMyCustomEvent={handler} />
.Check out docs. here https://lit.dev/docs/frameworks/react/
It is not clear from your question whether the react code that you posted lives inside a component or not. So to be extra clear, Here is an example of how that react file should look like:
Then you just call the component inside your app where you want it.