Skip to content

React basic theme switcher (Example usage of "context" in React)

Notifications You must be signed in to change notification settings

ericnjanga/React-Theme-Switcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Theme Switcher

Example usage of dynamic context in an React application.

The Concept

Users have the possibility to switch the application's theme (light or dark). The layout is based on Twitter's Bootstrap Jumbotron template.

How it works

  • Two objects describing "light" and "dark" styling are saved in a "settings" file which exports a "ThemeContext" object initialized with one of the objects const ThemeContext = React.createContext(themeStyle.dark).
  • ThemeContext is "provided" to the entire application by wrapping the root component with a ThemeContext Provider, <ThemeContext.Provider value={this.state.theme}>...</ThemeContext.Provider> which is initialized it with a default theme value (saved in the state)
  • ThemeContext is "consumed" by all components of the application (<ThemeContext.Consumer> ... </<ThemeContext.Consumer>)

Developer

Eric Njanga (LinkedIn | Medium)

About

React basic theme switcher (Example usage of "context" in React)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published