question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot be used new contextType API in constructor?

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

74reactions
awearycommented, Oct 24, 2018

@maisumakun how are you calling super in the constructor? If you pass context to super it’s accessible as expected.

https://codesandbox.io/s/l47jw5ryyz

class App extends React.Component {
  static contextType = Context
  constructor(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?

9reactions
OdenigboGodfreycommented, Mar 4, 2021

super(props, context) is deprecated now also :sigh

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found