0

I'm trying to create a base component that can handle UI themes. The theme is passed to components via context, but the base component that I've created is not receiving the context.

class DetailView extends ThemedComponent {
  constructor(props) {
    super(props);
  }

  render() {
    return <div>Detail View</div>;
  }
}

export default DetailView;

class ThemedComponent extends Component {
  constructor(props, context) {
    super(props);
    // context here is undefined
  }
}

ThemedComponent.contextTypes = {
  theme: PropTypes.object,
};

export default ThemedComponent;

I can get the context from DetailView if it extends Component directly and all that works fine. However, when I try to get the context within ThemedComponent it is always undefined.

What's the right way to create a common base component that can perform operations using the components context?

1
  • you have a typo: render { instead of render () { Commented Nov 10, 2015 at 21:38

1 Answer 1

1

You have to pass context to super in DetailView:

class DetailView extends ThemedComponent {
     constructor(props, context) {
          super(props, context);
     }
     .........
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.