🚀 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 8, 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

Share this post now:

💬 Comments (0)

Login to comment

Advertisement

Flaunt You Expertise/Knowledge & Help your Peers

Sharing your knowledge will strengthen your expertise on topic. Consider writing a quick Blog/Notes to help frontend folks to ace Frontend Interviews.

Other Related Blogs

Top 10 React Performance Optimization Techniques [React Interview]

Anuj Sharma

Last Updated Nov 3, 2025

Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.

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.

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 8, 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.

Implement Infinite Currying Sum: JavaScript Interview Question sum(1)(2)(3)

Anuj Sharma

Last Updated Nov 8, 2025

In this post, we will going to cover the step-by-step implementation of Infinite Currying Sum with a code example. This is one of the most common JavaScript Interview questions.

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 the frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

© 2024 FrontendGeek. All rights reserved