From the course: Building React and Django Apps

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Refactoring a higher-order component to use hooks

Refactoring a higher-order component to use hooks

From the course: Building React and Django Apps

Start my 1-month free trial

Refactoring a higher-order component to use hooks

- [Instructor] We're going to create a custom hook, useValidation, that will replace our FormField validation higher-order component. For this we need the useState hook. A custom hook is simply any function that begins with the word use. We're going to maintain the error state within this hook and create a validate callback. Then we create an ErrorDisplay component within this hook. If there is an error, we will return a div displaying it, and otherwise, will return null. And our hook will be returning the errorDisplay component and the validate method. Now, in the FormField, we're going to create a new component which uses this useValidation hook. Let's import the hook, and let's create our new FormField. So we use destructuring for this, and we get the ErrorDisplay component and the validate method. And now, we do what we did before, where we're getting the values that we need for display from the component…

Contents