Unlocking the Potential of React Hooks

Bishana Hennayake
2 min readJun 16, 2024

--

What Are React Hooks?

React hooks are functions that let you “hook into” React state and lifecycle features from function components. Prior to hooks, managing state and lifecycle methods was primarily done in class components, which could lead to bulky and less readable code. Hooks allow you to use these features in functional components, promoting cleaner and more modular code.

Core Hooks

React provides several built-in hooks that cover a wide range of use cases. Here are some of the most commonly used hooks:

useState

The ‘useState’ hook allows you to add state to functional components. It returns a state variable and a function to update that variable. Here’s an example:

import React, { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}

In this example, useState(0) initializes a state variable count with a value of 0, and setCount is the function used to update the state.

useEffect

The useEffect hook lets you perform side effects in functional components, such as data fetching, subscriptions, or manually changing the DOM. It’s similar to lifecycle methods like componentDidMount, componentDidUpdate , and componentWillUnmount in class components.

import React, { useState, useEffect } from 'react';

function Timer() {
const [seconds, setSeconds] = useState(0);

useEffect(() => {
const interval = setInterval(() => {
setSeconds(seconds => seconds + 1);
}, 1000);
return () => clearInterval(interval);
}, []);

return <div>{seconds} seconds have passed.</div>;
}

The useEffect hook takes two arguments: a function that performs the side effect and an optional array of dependencies. In this case, the interval is set when the component mounts and cleared when it unmounts.

useContext

The useContext hook allows you to consume context in functional components, making it easier to manage global state or theme settings.

import React, { useContext } from 'react';

const ThemeContext = React.createContext('light');

function ThemedButton() {
const theme = useContext(ThemeContext);

return (
<button style={{ background: theme === 'dark' ? '#333' : '#FFF' }}>
I am styled by theme context!
</button>
);
}

In this example, useContext(ThemeContext) provides the current context value (‘light’ or ‘dark’), allowing the component to render accordingly.

Benefits of Using Hooks

  • Simplified Components: Hooks enable functional components to handle state and side effects, reducing the need for class components and making the codebase simpler and more readable.
  • Reusability: Custom hooks allow you to extract component logic into reusable functions, promoting code reuse and separation of concerns.
  • Improved Performance: Hooks help avoid some common pitfalls with class components, such as excessive re-renders and complicated lifecycle methods, leading to better performance.
  • Reduced Boilerplate: Hooks reduce the amount of boilerplate code required for managing state and lifecycle events, allowing developers to focus more on the application logic.

--

--

Bishana Hennayake

As an IT undergraduate at the University of Moratuwa, I'm passionate about sharing knowledge with my peers.