DEV Community

Cover image for How YOU can use the Context API in React.js to pubsub

How YOU can use the Context API in React.js to pubsub

Chris Noring on June 16, 2019

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris The React Context exists so you don’t have to pass in data ...
Collapse
 
softchris profile image
Chris Noring ITNEXT

depends on what you want from your pubsub of course. If you want something use a React API I would say this is the one. If you want a straight up pubsub you can use anywhere I would look at a more generic approach like here, dev.to/angular/ngrx-from-the-begin...

Tbh I think the Context API looks a bit clunky with its Providers and Consumers components, but it's a supported way of doing it so I 'm thinking - should I use the supported way or come up with something myself

 
softchris profile image
Chris Noring ITNEXT

What I mean is there is obviously the React implementation for Redux. By using the library RxJS we can implement pubsub like so github.com/softchris/Architecting-... and we can probably shorten that to 4-5 lines if we really wanted to.. I find that React is prone to want to solve most things by creating a DSL of components... I prefer when things are more explicit and I can see what's going on. Obviously, the Context API works but it's just one way of many.. Pubsub exist in a lot of libraries across frontend and backend..