You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot be used new contextType API in constructor? #13944
Do you want to request a feature or report a bug?feature
What is the current behavior? Using Component.contextType, this.context keeps null in class component's constructor.
What is the expected behavior?
this.context can be obtained in constructor
if implementing of the feature is inappropriate, write in documentations that this.context cannot be used in constructor.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React 16.6 (older React doesn't have Component.contextType) in Chrome 70 (Windows 7)
classAppextendsReact.Component{staticcontextType=Contextconstructor(props,context){super(props,context)// this.context now works}
This seems consistent with this.props. If you don't pass props to super you cannot access this.props in the constructor. Should this.context work differently?
Reacted by Diego, maisumakun, Heaven, Jack Ford, Lyubomir, arianon, Hardik Modha, Owen Gillespie, liuguanhua, Hugo and 51 more
Reacted by Guillermo Zapata
Reacted by Lisandro Mindel, Armando Cincotti, Salim Semaoune, candice and Krishna Torque
Reacted by lzo, andrewnottaviano, Armando Cincotti, Salim Semaoune, Tautvydas, candice, Sarath, Krishna Torque and Evan Hsu
component class with a constructor signature constructor(props: MyProps, context: MyContext) is no longer valid JSXElementConstructor . So this usage is now prohibited by TS
Any workaround?
Reacted by Bruledamien, etsvetanov and Joshua Baker
Activity
sebmarkbage commentedon Oct 24, 2018
Hm. Yea. This is a bug I think. It should probably be allowed.
gaearon commentedon Oct 24, 2018
Want to send a failing unit test and/or fix?
aweary commentedon Oct 24, 2018
@maisumakun how are you calling
superin theconstructor? If you passcontexttosuperit's accessible as expected.https://codesandbox.io/s/l47jw5ryyz
This seems consistent with
this.props. If you don't passpropstosuperyou cannot accessthis.propsin the constructor. Shouldthis.contextwork differently?maisumakun commentedon Oct 25, 2018
@aweary Oh, thanks!
constructor(props, context)reminded me a code for legacy Context API, but necessary forcontextTypetoo. (this should be documented anywhere)gaearon commentedon Nov 1, 2018
Please feel free to send a documentation PR to section about using
contextType.https://github.com/reactjs/rfcs
OdenigboGodfrey commentedon Mar 4, 2021
super(props, context) is deprecated now also :sigh
molimat commentedon Jul 12, 2021
but actually almost the whole class component concept is deprecated. lol
chbdetta commentedon Jul 21, 2021
Simply untrue. There is no plan deprecating class component.
chbdetta commentedon Jul 13, 2022
According to:
https://github.dev/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L77-L79
component class with a constructor signature
constructor(props: MyProps, context: MyContext)is no longer validJSXElementConstructor. So this usage is now prohibited by TSAny workaround?
joshuakb2 commentedon Jun 26, 2023
You can access
this.contextfromUNSAFE_componentWillMount, but you'll get warnings in strict mode.This issue was closed previously because
super(props, context)was still available, but now it's deprecated. What's the way forward?kubijo commentedon Sep 30, 2023
Bumping this with the same reason ... if there is no new way of achieving this, this should not be deprecated.