Jon
Jon

Reputation: 15200

React 16.6.3 Context API contextType not being set

I have updated my local create-react-app to 16.6.3 (react and react-dom). I am trying to get an example of the contextType to work. Where I want to be able access the context from componentDidMount method. When I use the .Consumer part it works, but that is only within the render, I need to be able to get it and set some logic with it.

I have an example on codesandbox https://codesandbox.io/s/w08v196jww

I would expect the green.js class to be able to get the context from Green.contextType = AppContext and then be able to access it via the this.context approach. but the context always returns an empty object.

Upvotes: 3

Views: 314

Answers (1)

kenjiru
kenjiru

Reputation: 811

Seems like this problem is caused by a circular dependency. Extracting AppContext to a separate file should fix your issue.

Here is how it looks with your example: https://codesandbox.io/s/l49yw1ow4z

Upvotes: 1

Related Questions