By sgarcia.dev


2019-01-21 04:26:06 8 Comments

I am learning about the somewhat newly introduced React.Context API, but I've noticed some inconsistencies on it's consumption across examples. Some use the original Context.Consumer HOC method, while some (including the React Docs) use the static Class.contextType method. What's the difference and why the inconsistency?

1 comments

@sgarcia.dev 2019-01-21 04:26:06

Turns out that the static Class.contextType was newly introduced on React v16.6.0, as the Context.Consumer method proved problematic on class components. Also, there does seem to be one major difference between using both, and it's that the static Class.Context only allows you to subscribe to a single context.

The good news is that both of them seem to reliably listen of Context changes which means if you only have a single Context API, then both are good enough.

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] What is the difference between React Native and React?

64 Answered Questions

[SOLVED] Loop inside React JSX

  • 2014-04-05 05:29:28
  • Ben Roberts
  • 841872 View
  • 1301 Score
  • 64 Answer
  • Tags:   javascript reactjs

32 Answered Questions

[SOLVED] React js onClick can't pass value to method

  • 2015-04-22 23:40:14
  • user1924375
  • 537181 View
  • 646 Score
  • 32 Answer
  • Tags:   javascript reactjs

33 Answered Questions

[SOLVED] Programmatically navigate using react router

  • 2015-06-26 17:38:51
  • George Mauer
  • 757147 View
  • 1469 Score
  • 33 Answer
  • Tags:   reactjs react-router

19 Answered Questions

[SOLVED] Can you force a React component to rerender without calling setState?

  • 2015-06-03 16:44:58
  • Philip Walton
  • 836252 View
  • 700 Score
  • 19 Answer
  • Tags:   reactjs react-jsx

26 Answered Questions

[SOLVED] What do these three dots in React do?

  • 2015-06-25 11:21:52
  • Thomas Johansen
  • 277733 View
  • 922 Score
  • 26 Answer
  • Tags:   javascript reactjs

29 Answered Questions

[SOLVED] Show or hide element in React

  • 2014-07-01 05:17:00
  • user1725382
  • 652710 View
  • 546 Score
  • 29 Answer
  • Tags:   javascript reactjs

1 Answered Questions

Refactoring logic outside of Context.Consumer

  • 2019-10-20 14:06:56
  • Bridger Reif Hammond
  • 15 View
  • 0 Score
  • 1 Answer
  • Tags:   javascript reactjs

5 Answered Questions

3 Answered Questions

[SOLVED] React with Redux? What about the 'context' issue?

Sponsored Content