How to Use a Hook in a Class Componentby@nickytonline
2,164 reads

How to Use a Hook in a Class Component

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

You can't use hook directly in a class component, but you can use a hook in a wrapped function component with a render prop to achieve this. The beauty of this pattern is you can build new components as function components using hooks. Class components that can't be upgraded for whatever reason benefit from the same functionality via a thin compatibility layer, the wrapper component. Let's first create a hook that has a render prop called render that will expose the value and setter for the custom hook.

Company Mentioned

Mention Thumbnail
featured image - How to Use a Hook in a Class Component
Nick Taylor HackerNoon profile picture

@nickytonline

Nick Taylor

Learn More
LEARN MORE ABOUT @NICKYTONLINE'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa