DEV Community

Discussion on: When Should You Convert Your Classes to Hooks??

Collapse
sberney profile image
Sam Berney

Thanks Adam! This post has really helped me convert my react classes to hooks. I'm only left wondering why I can't use useContext hooks in my classes.

Collapse
bytebodger profile image
Adam Nathaniel Davis Author

I freely admit that useContext is one of the benefits of Hooks. You can use the Context API with Hooks or with class-based components. But useContext just "feels" easier.

IMHO, the biggest annoyance of the Context API in classes is that it's very obtuse to figure out how you would consume multiple contexts inside a single class. If you're not familiar with it, I outlined the process here: dev.to/bytebodger/a-context-api-fr...

The Cliff's Notes are this:

  1. import however many contexts you need to consume in the class.

  2. Grab the current values by using contextName = SomeContext.Consumer['_currentValue'];

  3. Reference any of the variables that exist in that context.