What to do if one sees no console logs from LWC after applying window.addEventLister?

378    Asked by ChristianParsons in Salesforce , Asked on Aug 30, 2023

 I am trying to build an LWC to listen to an event from window.postMessage by using window.AddEventLister. Here is a code I used:

After using this code, when I use the Chrome console to post any message to the window, I don’t see any logs from LWC. I have used Add.EventListener and this. addEventListener. What am I doing wrong? 

You need to use window.removeEventListener and window.AddEventListener. Also, you can use connectedCallBack and not constructor like the code below:



The arrow function keeps this reference to a class. So, if you use a regular function, you must use ‘bind .’Also, remember that you need to store the original event handler (#receiveMessageCallback) to get the memory when the components unload.



Your Answer

Interviews

Parent Categories