user-avatar

Kyle J. Roux

jstacoder

Developer Program Member

a python/javascript/php/linux guru ready to take on the world...

CasePeer
orange, CA, USA
https://jstacoder.github.io

Hi
Home

Dark mode hook

1 min read

Dark Mode:

The react hooks way

Here I will show how to use reacts new useContext and useReducer hooks by adding a dark mode toggle to a react app

To start out we will need three things:

A light colored theme object

A dark colored theme object

A way to provide the theme to your components

For the themes i prefer to combine them into a single themes object

themes.js
import { dark } from './themes/dark'
import { light } from './themes/light'
export const themes = {
dark,
light,
}

And for the themes i like to define general names for colors to use

themes/light.js
export const light = {
colors: {
lightText: '',
darkText: '',
lightBackground: '',
darkBackground: '',
primary: '',
secondary: '',
},
}
Home