Dark Theme With React

İlkay Yüksel
2 min readOct 13, 2021

--

In this article, I want to explain how to create a dark theme with React.

Firstly I created ThemeContext type.

I created context using ThemeContext type. I did the process of changing the value of the mode variable in the state in the toggle function required to be able to change the mode. I added the outermost Context Provider while returning the JSX expression that this component will render.

I defined the background and text colors of the dark and light themes in the theme.ts file.

I created a Wrapper component so that the theme change takes effect in every component of the project. I wrapped this component with the ThemeProvider that I created in the ThemeManage folder.

I created a button component to enable theme change. I gave the toggle function in the context of the onClick event of this button. I made the button icon change according to the theme with the help of conditional rendering.

I created a component for the background image. I made the background image change according to the theme with the help of conditional rendering.

I used the dark theme by wrapping the Todo App with ThemeProvider.

Here is how the dark and light theme looks in Todo App:

Dark mode
Light mode

You can find Todo App’s code on my GitHub:

--

--

No responses yet