1

I have this peace of vanilla js code that needs to notify a React component for a certain event. How to do it?

Please note, I do not want to read about Flux/Redux, Thinking in Components, etc. I just want a plain and simple one-liner for communication between Javascript code and React. Probably I am looking for some kind of pub/sub pattern without relying on another 3rd party library.

SM79
  • 1,234
  • 2
  • 15
  • 33

1 Answers1

3

It turns out it is answered here

Javascript side:

var evt = document.createEvent("Event");
evt.initEvent("myEvent",true,true);
evt.foo = "bar";
document.dispatchEvent(evt);

React side:

document.addEventListener("myEvent",myEventHandler,false);
Community
  • 1
  • 1
SM79
  • 1,234
  • 2
  • 15
  • 33