Custom Hooks
Part of the React course
You wrote a drawer that opens and closes. Three useState lines and a handler that flips a boolean. Then the same thing for a dropdown, and again for a modal, and again for a "show more" link. Four components now contain the same six lines, and when you decide the toggle should also remember its last state, you get to change them all and miss one.
A custom hook is the fix, and it is much less exotic than the name suggests. It is a normal JavaScript function whose name starts with use and which calls React's own hooks inside it. That is the entire definition. There is no special syntax, no registration, nothing to import from React to declare one. ...
Want to learn React Custom Hooks in a real environment?
Click below, get a fresh Linux box with React ready to go, and work through Custom Hooks hands-on with an AI tutor that knows your code.
Start this lessonCustom HooksWhat you'll learn
- Extracting stateful logic into a function
- Use prefix convention
- UseToggle example
- UseLocalStorage example
- Hooks rules
Read this lesson in your language
All lessons in this course
17 lessons- 1What is React?25 min
- 2JSX and Components30 min
- 3Props30 min
- 4State with useState35 min
- 5Handling Events30 min
- 6Conditional Rendering and Lists30 min
- 7Forms and Controlled Inputs35 min
- 8Effects with useEffect40 min
- 9Data Fetching40 min
- 10Component Composition35 min
- 11Context and useContext35 min
- 12useReducer35 min
- 13Custom Hooks35 min
- 14Performance and Memoization35 min
- 15Routing with React Router40 min
- 16Testing React Components40 min
- 17Capstone: Todo App60 min
