2

What I have

I am trying to adapt a web designed library to react native, this implies that certain things must be replaced like the use of custom events, I want to implement this behaviour, create and dispatch/trigger custom events in RN.

I have reviewed some answers to similar questions, but there are some options to simulate custom events that are no longer works by the new react-native versions, since React Native doesn’t include the Node Standard Library.

What I want

this is a piece of code in which custom events are implemented and captured, this is what I want to simulate in RN.

const authEvent = new Event('auth');

// Listen for the event.
window.addEventListener('auth', function (e) { /* ... */ }, false);

// Dispatch the event.
el.dispatchEvent(authEvent);
Cristian Flórez
  • 2,277
  • 5
  • 29
  • 51
  • Have you considered using a framework like react native redux? Once you get past some boilerplate setup, it does a decent job. – Atmas Mar 15 '21 at 01:54
  • I'm using Apollo Client and Graphql, so use redux can be redundant – Cristian Flórez Mar 15 '21 at 23:44
  • Yes, I can see how that might be. I haven't coded yet with Apollo and Graph, but it does look both interesting and incompatible as you say. Have you tried looking into a 3rd party solution? Something like https://www.npmjs.com/package/react-native-event-listeners? – Atmas Mar 16 '21 at 23:43
  • Hey, have you found a solution for this? I'm looking for the same concept. – Patryk Szylin Aug 19 '21 at 10:33
  • @Atmas Our project does use Redux. How can I leverage it to perform the same function as in OP's post? – Sturm Dec 09 '21 at 16:02
  • You could store auth as a redux state. You can dispatch (as a first class concept) messages to redux to update the auth state (say after authentication completes in a callback). You then code components which are bound to the redux state and that binding implicitly gives you access to the auth state via props that fire render updates whenever your state changes. – Atmas Dec 09 '21 at 20:28

0 Answers0