afzalsiddique

ThemeContext.js

Sep 18th, 2021
609
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useContext, useState} from "react";
  2. const ThemeContext = React.createContext()
  3. const ThemeUpdateContext = React.createContext()
  4.  
  5. export function useTheme(){ return useContext(ThemeContext) }
  6. export function useThemeUpdate(){ return useContext(ThemeUpdateContext) }
  7.  
  8. export function ThemeProvider({children}){
  9.   const [darkTheme,setDarkTheme] = useState(true)
  10.   const toggleTheme = ()=> setDarkTheme((prevDarkTheme)=>!prevDarkTheme)
  11.  
  12.   return (
  13.     <>
  14.       <ThemeContext.Provider value={darkTheme}>
  15.         <ThemeUpdateContext.Provider value={toggleTheme}>
  16.           {children}
  17.         </ThemeUpdateContext.Provider>
  18.       </ThemeContext.Provider>
  19.     </>
  20.   )
  21. }
  22.  
RAW Paste Data