Hooks are a mechanism in React that allows you to work completely without classes. It doesn't bring anything new, but it makes it easier to reuse code to solve common problems. Currently, this is the main way to write React applications. But hooks don't replace classes entirely. Moreover, the React team doesn't plan to remove support for classes, and some things can't be done without classes. An example of how the useState
hook works for storing the state:
// useState – built-in React hook
// To be discussed in more detail in the next lesson
import React, { useState } from 'react';
const Example = () => {
// Example of a hook for working with the state
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} time(s)</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
};
Hooks are functions whose names usually begin with use. So they're easy to distinguish from any other functions. React has about 10 hooks built in, of which only a few are used regularly. The main ones are those that repeat the functionality of class components, such as working with state, side effects (lifecycle), context, and direct access to the Dom. We'll look at them throughout the course. Information on the remaining hooks can be found in the official documentation.
In addition to the built-in ones, you can find hundreds, if not thousands, of ready-made hooks for all purposes on the web. For example, the popular react-use has more than 115 hooks. Now, development in React has largely turned into the search and use of suitable hooks, which is good, you can focus on doing your important business, and worry less about inventing bicycles.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.