What is a Hook?
Our ability to "hook" into React's state and lifecycle routines is made possible by hooks.
With React Hooks we can control the internal state of components and handle side effects after rendering directly from our function components. If you want to use React features that don't require classes, you'll need to use hooks instead. Class components aren't being replaced by function components or React Hooks. Using them is absolutely optional; it's merely a new tool we can use.
A look at React Hooks reveals that they solve numerous issues, particularly when it comes to huge online apps. React 16.8 introduced hooks, which let us make use of state.
UseState
The State Hook is a named export from the React library, so we import it like this:
Stateful value and function for updating it are returned. Using useState in a function component allows you to use a local state. It returns a variable with the current state value (which may or may not be the original state) and a function to update this value, which you can supply to this function.
Whenever a state is updated, all of the state's data is wiped clean.
React's useState() function is a JavaScript one. An array with the following values is returned when this function is called:
We can use a function called.state setter to alter the value of this state's.current state property.
As an illustration, consider the following function component that makes use of the State Hook:
UseEffect
It is possible to add additional effects to function components using the Effect Hook.
we’ll use the Effect Hook to run some JavaScript code after each render, such as:
.fetching data from a backend service
.subscribing to a stream of data
.managing timers and intervals
.reading from and making changes to the DOM
There are three key moments when the Effect Hook can be utilized:
.When the component is first added, or mounted, to the DOM and renders
.When the state or props change, causing the component to re-render
.When the component is removed, or unmounted, from the DOM
Next, let's check out the useEffect hook. Hooks can be used to control the state and side-effects of function components, as previously stated. The useState hook served as a demonstration of state management. The useEffect hook now comes into play for side-effects, which are typically used for interactions with the Browser/DOM API or an external API, such as data fetching.
The useEffect() function returns nothing because the Effect Hook is used to call another function that performs anything for us.
React Context
Global state management is possible using React Context.
Sharing state between deeply nested components becomes easier when used in conjunction with the useState Hook.
Create Context
To create context, you must Import createContext and initialize it:
In order to use the Context in a child component, we need to access it using the useContext Hook.
UseReducer
In this case, The reducer hook is employed in the case of more complicated state diagrams. With array destructuring, it accepts a reducer function as input, returns the current state, and then returns a dispatch function as the result of the operation.
A similarity exists between the useReducer Hook and the useState Hook.
It's flexible enough to support user-defined state machines.
UseReducer may be handy if you need to maintain track of numerous pieces of state that are dependent on sophisticated reasoning.
UseCallback
Memoized callback functions are returned by the React useCallback Hook.
Memorization can be thought of as storing a value in memory so that it is not recalculated each time.
These functions can be isolated so that they don't run every time a render occurs.
When one of its dependents is updated, the useCallback Hook is activated.
This could help you perform better.
UseCallback can be used to prevent a component from re-rendering if its props have changed.
The useCallback hook is utilized if you have a component in which the child is re-rendering itself over and over again, and you don't know why.
An inline callback and a list of dependents can be passed. It will only change if one of the dependents has changed if you call useCallback on it. In the case of child components that rely on reference equality to avoid wasteful renderings, this can be advantageous.
UseMemo
Memoized values are returned by the React useMemo Hook. Memorization can be thought of as storing a value in memory so that it is not recalculated each time. Only when one of its dependencies is updated does the useMemo Hook get activated.This could help you perform better.UseMemo is a hook in React that allows you to cache expensive functions so that they don't have to be run every time the page is rendered. UseMemo will only recompute the memoized value if one of the inputs has changed. You simply supply in a function and an array of inputs.
The difference between usememo and usecallback:
The useMemo and useCallback Hooks are similar. The main difference is that useMemo returns a memoized value and useCallback returns a memoized function.
useRef
Values can be carried over from one render to the next using the useRef Hook. In this way, a value that may be changed without affecting the appearance of the page can be stored. It can be used to directly interact with a DOM element.Use the useRef Hook to maintain track of earlier state values as well.
React Custom Hooks
Reusable functions are what hooks are. Hooks can be used to retrieve functionality from a component that has to be used in numerous components. The first word in a custom hook is "use." FriendStatus is a good example.
Hooks don't require the same type of signature as React components. We have complete control over what it accepts as arguments and what it returns. In other words, it's a typical procedure. So that you can see right once that it adheres to Hooks, its name should always begin with the word "use."
Summary
It is possible to use function components as hooks to access state and lifecycle elements in the React framework from within those components. Hooks allow you to use React without the need for classes because they don't work inside classes. You can start utilizing Hooks in new components if you'd like, but we don't recommend changing your old components overnight.). Hooks are likely to make your code more reusable and easier to maintain. I hope this blog has helped you to better grasp hooks in the end. You can find more of my writings on Twitter, so be sure to follow me there if you enjoy them.