paint-brush
How to Use a Hook in a Class Componentby@nickytonline
8,107 reads
8,107 reads

How to Use a Hook in a Class Component

by Nick Taylor3mApril 5th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

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
Nick Taylor

Nick Taylor

@nickytonline

I am a Lead Software Engineer with a focus on the front-end.

Learn More
LEARN MORE ABOUT @NICKYTONLINE'S
EXPERTISE AND PLACE ON THE INTERNET.
L O A D I N G
. . . comments & more!

About Author

Nick Taylor HackerNoon profile picture
Nick Taylor@nickytonline
I am a Lead Software Engineer with a focus on the front-end.

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite