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.

Function components do not support contextType.

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Warning: withRouter(RegisterModal): Function components do not support contextType.

What is the expected behavior?

No error

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.6.0

https://codesandbox.io/s/01zl4m022v

Unfortunately, I’m unable to reproduce this in codesandbox…

import React, { Component } from "react";
import { withRouter } from "react-router-dom";
import ErrorContext from "./ErrorContext";

class RegisterModal extends Component {
  render() {
    return <div>test</div>;
  }
}

RegisterModal.contextType = ErrorContext;

// this doesn't cause any error:
// export default RegisterModal;
// this causes an error:
export default withRouter(RegisterModal);

For some reason, whenever I wrap my RegisterModal component with withRouter, I get this error: Warning: withRouter(RegisterModal): Function components do not support contextType.. The weird thing is I’m setting contextType directly on the class, before the HOC function is applied.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
bjm88commented, Nov 13, 2018

was usings contextType = ThemeContext for example on my component as context api suggests, was on react 16.6.1 and it still had this issue…

7reactions
kiranvjcommented, Nov 28, 2018

Yes, the issue persists even after updating hoist-non-react-statics. See the issue discussed here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function components do not support contextType
I encountered an issue when trying to add context to a React component that was wrapped with a React Router withRouter(...) function. import ......
Read more >
Warning when using withRouter with new React Context API
Warning: withRouter(Home ): Function components do not support contextType. This happens when use the React context API like this
Read more >
Context | React
Stateless functional components are also able to reference context if contextTypes is defined as a property of the function. The following code shows...
Read more >
React's Context API Tutorial: What Context is and How to use it!
While this solution does work, problems begin if a component on a different ... I have Warning: Image: Function components do not support...
Read more >
contextType property of a React component ... - DeepScan
createContext() are assigned to the contextType property. Note that you should use the context object itself, not the Consumer or Provider ...
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