🚀 AI SaaS Starter is now live!

50% OFF

Use code FIRST50

Blog/NotesConcept

Implement useTheme() Custom Hook in React (Interview)

Explore implementation of most asked useTheme custom hook in react to handle the theme of the application.

Beginner

Anuj Sharma

Last Updated Nov 23, 2025


Nowadays theming is an essential part of the most web application that allowing users to customize the appearance of an application according to their preferences. This is one the important functional implementation when it comes to frontend development.

In this blog post, we will dive into implementing the useTheme() custom hook in React, a commonly discussed topic in frontend interviews as well.

Implementation of useTheme() custom hook

Before jumping into the useTheme() implementation, its important to know how useState() and useEffect() hooks work in react.

Let's start by creating a new custom hook named useTheme() that will handle the theme functionality. We will use the useState hook to manage the theme state and the useEffect hook to apply the selected theme.

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

const useTheme = () => {
    const [theme, setTheme] = useState('light');

    useEffect(() => {
        document.body.className = theme;
    }, [theme]);

    return [theme, setTheme];
};

export default useTheme;

Theme Selection using useTheme() custom hook

Now, let's create a component that uses the useTheme() custom hook to allow users to toggle between light and dark themes

import React from 'react';
import useTheme from './useTheme';

const ThemeSelector = () => {
    const [theme, setTheme] = useTheme();

    const toggleTheme = () => {
        setTheme((prevTheme) => prevTheme === 'light' ? 'dark' : 'light');
    };

    return (
        <div>
            <button onClick={toggleTheme}>Toggle Theme</button>
            <p>Current Theme: {theme}</p>
        </div>
    );
};

export default ThemeSelector;

Usage Explanation

To use the useTheme() custom hook in your application, follow these steps:

  1. Create a new React component (e.g., ThemeSelector) and import the useTheme() custom hook.
  2. Use the useTheme() hook in the component to manage the theme state.
  3. Implement the desired theme selection logic within the component.
  4. Render the component in your application to allow users to toggle between themes.

Final thoughts

In this blog, You have successfully go through the implemented and usage of the useTheme() custom hook in React to handle theme functionality.

By mastering custom hooks like useTheme(), you can enhance the user experience and streamline the development process.

Remember to practice implementing custom hooks and explore different use cases to deepen your understanding of React development. This will help in both react development and frontend interviews.

Keep learning, and happy coding :)

Learn Next 🚀

  1. 20 Most Asked Custom Hooks in React for Interviews
  2. Best Resources to prepare for React Interviews

🚀

Love this content? Share it!

Help others discover this resource

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀

Comments

1 comment

Guest User

Please login to comment

0 characters


Anuj Sharma

anuj.mangocoder@gmail.com

10 Nov, 2025

Great !! to the point blog 🚀


Share Your Expertise & Help the Community!

Build Your Portfolio

Help the Community

Strengthen Your Skills

Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀


Other Related Blogs

Implement useToggle() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Dec 23, 2025

Explore code explanation of useToggle() custom hook in react to handle the toggle event efficiently.

Polyfill for map, filter, and reduce in JavaScript

Anuj Sharma

Last Updated Oct 2, 2025

Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.

Flatten Nested Array in JavaScript using Recursion

Anuj Sharma

Last Updated Nov 24, 2025

Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

Implement useFetch() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Nov 23, 2025

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

© 2025 FrontendGeek. All rights reserved