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.

16.6 Context API not working in class component

See original GitHub issue

Do you want to request a feature or report a bug? Quite possibly a bug (or maybe confusion about the current API)

I am using the new Context API as well as the new static contextType in React 16.6. I am passing context down a couple components deep but when I attempt to access the context within the component, the object is empty (~only the default value passed into createContext is being displayed~). This is happening in a current feature I am working on at my job, so I cannot display that code, but I did create a Codesandbox with the gist of the problem.

Here is a demonstration of the behavior: https://codesandbox.io/s/r4myz959ro

I would expect to be able to access the current values of the context. This way, if those values change, I would always have the most recent values. Now, maybe this is expected behavior, however, it would be confusing if it is.

React 16.6 ReactDOM 16.6

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:29
  • Comments:37 (10 by maintainers)

github_iconTop GitHub Comments

43reactions
awearycommented, Oct 26, 2018

@dericgw the problem you’re describing has nothing to do with React, it’s the behavior of circular ES6 module imports. Here’s an example showing the same problem without React.

The index file imports View, which imports the index file and the AnotherLevelDeep, which itself requires the index file again. It’s a circular dependency.

The reason it works with the Context.Consumer API is that the render method is lazily evaluated, while the class definition (including static properties) are evaluated when the module is loaded.

26reactions
lxcidcommented, Oct 29, 2018

Did u manage to resolve it? I’m facing similar issue.

context is an empty object {} even if I did something like this

const Context = React.createContext();

class Child extends React.Component {
  render() {
    const renderedContext = this.context;
    console.log({ this: this });
    console.log(renderedContext);
    console.log({ asd: Child.contextType });
    return <div />;
  }
}
Child.contextType = Context;

export const Test = props => (
  <Context.Provider value={{ hello: 'world' }}>
    <Child />
  </Context.Provider>
);

I’m quite sure I have no cyclic rendering, make sure I’m on 16.6.0 but nothing seems to work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React - New Context API not working with Class.contextType ...
It doesn't work when I use the Class.contextType = Context approach: return function myHOC(WrappedComponent) { class HOC ...
Read more >
Context - React
The contextType property on a class can be assigned a Context object created by React.createContext() . Using this property lets you consume the...
Read more >
How to use React Context like a pro - Devtrium
React Context can be a very powerful tool, and there are several tricks to using it effectively. Several patterns can be used to...
Read more >
Using Context API with Class Components - CodeSandbox
Using Context API with Class Components ... Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >
React Context API From Scratch A-Z - Medium
React context is great when you are passing data that can be used in any component in your application. Props drilling problems can...
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