Hooks in React

ยท

4 min read

Hooks in React

Today we'll learn about the widely used hooks in React. First, let me tell you the need for hooks. As we all know React has functional and class components. Previously, the functional components were only used to render the data but didn't consist of the business logic or any side effect in itself.

So hooks were introduced to implement lifecycle methods and states in functional components. They also bought the idea of code reusability along with them, that's why it was so quickly accepted within the React Community.

Drum rolls ๐Ÿฅ

Let's move ahead with the main topic.

  • useState hook

This hook helps us to use and maintain a state within the functional component. It's very easy to use. The useState hook returns an array having two elements, the first one is the state variable and the second is the function that modifies the state variable.

Here, we have number as the state variable and setNumber as the function which will modify the number.

  • useEffect hook

This hook works as a replacement for all the lifecycle methods which were used in class components. It contains a callback and a dependencies array. The hook will run every time when any of the values in the dependencies array changes.

Here as you can see, the dependencies array contains number. So every time the user presses any of the buttons or changes the input value, the useEffect hook will trigger and it'll check the number whether it's odd or even.

Within this hook, you can call side effects such as changing something in DOM, fetching data from an API, etc.

One more thing used in this hook is the cleanup function, which runs when we return a function in the callback. This function helps in cleaning of side effects i.e clearing a timer, closing a web socket, etc.

  • useContext hook

As in the class components, we had the Context API in the same way, we have useContext hook in functional components. This hook basically helps you to maintain a common global state across components. When the state changes it triggers re-renders across all the child components under the Context, even if the parent component uses shouldComponentUpdate or React.memo.

This hook can be used to implement a loading state within the application, passing data to children components, etc.

To create it, we use the createContext function and pass the initial value. In the context provider, we pass a prop called value which consists of the context variables and functions to modify the context.

Then we use this context in our Parent Component and wrap our app inside the Context Provider and pass a value. So now all the child components have the access to the value prop.

Now in the child component Person we can access the value prop and we can change the context value too.

This way you can use the useContext hook. Make a context, wrap the parent component with the context's provider and you are good to go!

You can experiment with the above code here.

  • useReducer hook

If you know Redux then you already what this hook does, if not then simply it changes the state according to the action passed. It helps in maintaining a local state easily whereas in Redux we can do that globally.

To use the reducer globally, we can make a context and pass it across the components. Geddit!

Here we have an initial state and a dispatch to modify the state. Within the dispatch, we pass actions along with some info called payload. The action then goes to the reducer which in turn changes the state.

You can find the above code here.

That's it for now. There are other hooks too like useRef, useCallback but they are rarely used. Thanks for reading this article. Let me know if anything is not clear.

Connect with me on Twitter, Instagram & LinkedIn

Happy Coding!

Did you find this article valuable?

Support Deepansh Bhargava by becoming a sponsor. Any amount is appreciated!

ย