I'm pretty new to React, and I would essentially have a service (one single instance) that I could somehow inject and use in multiple React components.
So far, the options I've come across by googling were:
- Using React's context, but it's not recommended, because it is an undocumented API.
- Passing the object along in the props, from component to component, but it feels a bit inelegant and tedious
- Using an IoC container, such as inversifyJS, who looks great, only it relies on typescript, and I don't wish to write my React app in typescript.
Now, Inversify can apparently be used in vanilla JS, with inversify-vanillajs-helpers
, but when I tried using it in React to resolve a component (my App
component), it kept throwing an exception stating
"Missing required @injectable annotation in: ReactComponent"
So, I'm trying to figure out a way to get an instance of my service (the same instance shared across the few components that use it), either by making inversify work with React but without typescript, or a new approach I haven't explored yet.
Any ideas ?