RSS Amplifier

James' Blog · May 25, 2017

React context require contextType

0
Sign in to vote or save

jamesbaum.co.uk

I don't often use `context` when working with React, but today I came across a project which was using it. I was trying to add a new class but the context passed in was always empty, and I couldn't work out why - most of the code was identical to a class which was working.

The only difference was that the working class had defined contextTypes:

export default class Hello extends React.Component {
  static contextTypes = {
    store: React.PropTypes.object,
  };
}

When I added contextTypes to my class, suddenly the context was available! A bit more digging showed that this is a known quirk, and in fact is mentioned in the docs (but only in very small writing!).

Read the original on jamesbaum.co.uk

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.