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

20 Most Asked Custom Hooks In React for Interviews

Anuj Sharma

Last Updated Jun 27, 2026

Explore the Most Common Custom Hooks in React asked in the React Interviews. It includes the code example of all the custom hooks in react for a quick revision before interview.

How to create custom useInfiniteScroll Hook in React

Anuj Sharma

Last Updated Jun 20, 2026

Learn how to implement useInfiniteScroll hook in react to handle long list of items efficiently using intersection observer internally.

Implement Custom useKeyPress Hook in React

Anuj Sharma

Last Updated Jun 11, 2026

Explore the code implementation of the custom useKeyPress Hook to monitor the key press events in React that can help to integrate actions based on the key press.

Implement Custom useMediaQuery Hook in React

Anuj Sharma

Last Updated Jun 11, 2026

Explore code implementation of the custom useMediaQuery hook to handle the website responsiveness for different devices using common custom hook code

Implement custom useScrollPosition Hook in React

Anuj Sharma

Last Updated Jun 11, 2026

Explore the code implementation of the custom useScrollPosition hook to monitor the scroll position to invoke actions based on the scrolling in reactjs.

Implement custom useClipboard() Hook in React

Anuj Sharma

Last Updated Jun 11, 2026

Code implementation of custom useClipboard hook to manage copy the values to the clipboard with detailed explanation.

React useCopyToClipboard Hook: Explanation & Usage

Anuj Sharma

Last Updated Jun 11, 2026

Explained the step-by-step implementation and usage of custom useCopyToClipboard Hook to manage the interaction with the clipboard in react app.

Implement Custom useIntersectionObserver Hook in React

Anuj Sharma

Last Updated Jun 11, 2026

Explore the code implementation of a custom useIntersectionObserver hook in React, which helps to manage the infinite scroll in a React application.

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 →

From Creator

© 2026 FrontendGeek. All rights reserved