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 Feb 21, 2026


Implement useTheme() Custom Hook in React (Interview)

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 Blog? Share it Now!

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  🚀


Learn Next

Featured

100+ Top React JS Interview Questions And Answers

20 Most Asked Custom Hooks in React for InterviewsTop 10 React Performance Optimization Techniques25 Top JavaScript Interview Questions for BeginnersHow to create custom useInfiniteScroll Hook in ReactImplement useThrottle Custom Hook In React

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 🚀


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  🚀

Share your expertise

Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.

Build your portfolio

Help the community

Sharpen your skills

Earn goodies

Other Related Blogs

React Hook Rules: Why hooks declarations are not allowed inside functions

Frontendgeek

Last Updated Feb 6, 2026

A quick guide to explain an important react interview question, why React Hooks declarations are not allowed inside functions or any conditional blocks with code example.

Implementing a stopwatch using React - Frontend Machine Coding Question

Pallavi Gupta

Last Updated Feb 21, 2026

Concise explanation of stopwatch implementation using React, it involves the usage of useEffect hook for creating a stopwatch and tracking milliseconds.

Implement useClickOutside() custom Hook in React [Interview]

Anuj Sharma

Last Updated Dec 23, 2025

Understand the implementation of useClickOutside() custom hook in react and how it can be used to implement Modal like functionality.

Best Frontend System Design Interview Cheat Sheet 📒

Anuj Sharma

Last Updated Jun 9, 2026

A Comprehensive Frontend System Design Cheat Sheet helps you approach the Frontend System Design Interview in the most structured way and covers the 7 most important Frontend System Design Topics.

Stay Updated

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

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewFrontend JobsQuestionsNewInterview ExperienceBlogsToolsLeaderboardFrontendGeek Chrome extensionGet the extension on the Chrome Web Store →

© 2026 FrontendGeek. All rights reserved