Does ReactJS have a publisher / subscriber model similar to EventEmitter in node?
Asked
Active
Viewed 969 times
2 Answers
1
No, ReactJS does not have an event emitter-like implementation. The core framework is focused on UI. If you need more complex events than callbacks, there are many available as npm packages.

WiredPrairie
- 58,954
- 17
- 116
- 143
-
Do you have any recommendations as to how I would go about doing something like this: I have a React class called User that has been instantiated multiple times in my UI, some of EventEmitter emits an event 'event', and I would now like to change the state of all my instantiated User objects? – Olshansky Apr 06 '15 at 16:34
-
People use the "Flux" pattern to communicate top-down changes. http://facebook.github.io/flux/docs/overview.html – WiredPrairie Apr 06 '15 at 19:45
-
Great, that's what I was looking for! – Olshansky Apr 06 '15 at 22:13
0
You want to investigate Flux. It provides a data modeling pattern that pairs nicely with react.
Essentially, you generate actions from a react component (or perhaps a push notification from the server) the action is picked up by a dispatcher, and your data stores register subscriptions (callbacks) with the dispatcher. In turn, your store can then update the state appropriately and any react components listening for change events on the store will be notified.
It is effectively pub sub with some extra administration to ensure one directional data flow.

Michael Ross
- 47
- 2
- 10