How to Use a Hook in a Class Componentby@nickytonline
7,421 reads

How to Use a Hook in a Class Component

tldt arrow
Read on Terminal Reader
Read this story w/o Javascript

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

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


Receive Stories from @nickytonline

react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!