Cannot be used new contextType API in constructor?
See original GitHub issueDo 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.contextcan be obtained in constructor- if implementing of the feature is inappropriate, write in documentations that
this.contextcannot 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)
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
is there a way to access new context api within constructor?
I totally agree with jagster answer. As well I would add that, if you do not use the props parameter in the constructor(props)...
Read more >Context - React
Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the...
Read more >FileContext Constructor (Microsoft.VisualStudio.Workspace)
Initializes a new instance of the FileContext class. ... public FileContext (Guid providerType, Guid contextType, object context, System.Collections.
Read more >SearchBar | @jupyterlab
If using the new style context, re-declare this in your class to be the React.ContextType of your static contextType . Should be used...
Read more >Context - grammY
The has checks correctly narrow down the context type. ... We can use the new type by passing it to the Bot constructor....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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?super(props, context) is deprecated now also :sigh