-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Injection fails inside the constructor of React components #1
Comments
Thanks! I should update the docs. This library use TL;DR @provider(Session)
class App extends React.Component {
render() {
return <Loader />;
}
}
class Loader extends React.Component {
@inject session: Session;
constructor(props, context) {
super(props, context);
this.session.doSomethingUseful();
}
} Also, if you use |
Oooh I see, thanks for clarifying! Will try it out this evening to see if it works.
… On 15 Jan 2019, at 09:34, Dmitry Panyushkin ***@***.***> wrote:
Thanks! I should update the docs.
This library use React.Context under the hood. And if you want to use dependency inside constructor — you should pass the context argument to super(). See explainaition <https://overreacted.io/why-do-we-write-super-props/> from Dan Abramov.
TL;DR
If you don't pass props or context to super() React assign it to this.props and this.context immediately after your component constructor call. But if you want to use it inside constructor — you should call super(props, context).
@Provider(Session)
class App extends React.Component {
render() {
return <Loader />;
}
}
class Loader extends React.Component {
@Inject session: Session;
constructor(props, context) {
super(props, context);
this.session.doSomethingUseful();
}
}
Also, if you use @Inject decorator, you don't need to manually specify static contextType = InjectorContext. The decorator is already doing this for you.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#1 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ACpK647OSIABinpFu-WMIHk3Z2VlC7Cmks5vDZKugaJpZM4Z3rpi>.
|
I wish I could give this project more stars, because it has saved me a lot of time and made my code much cleaner. Thanks for the awesome library!
There's one issue I'm having, though. I'm trying to use a dependency inside the constructor of a React component, and somehow the library always complains with the following error:
I've set up everything in accordance with the documentation (I think).
Is there something I'm missing, or is this use case currently not supported by the library? Everything works fine if I defer accessing the dependency in
<Loader />
tocomponentDidMount
, so I'm guessing this is either a bug or a limitation.The text was updated successfully, but these errors were encountered: