Quellcode durchsuchen

fe-21 使用context实现暗黑模式功能

master
Chunxian Zhang vor 2 Jahren
Ursprung
Commit
3244b5dd63
1 geänderte Dateien mit 40 neuen und 0 gelöschten Zeilen
  1. +40
    -0
      lazy-timer-fe/src/context/ContextProvider.js

+ 40
- 0
lazy-timer-fe/src/context/ContextProvider.js Datei anzeigen

@ -0,0 +1,40 @@
import React, { createContext, useContext, useState } from 'react';
const StateContext = createContext();
const initialState = {
chat: false,
cart: false,
userProfile: false,
notification: false,
};
export const ContextProvider = ({ children }) => {
const [screenSize, setScreenSize] = useState(undefined);
const [currentColor, setCurrentColor] = useState('#03C9D7');
const [currentMode, setCurrentMode] = useState('Light');
const [themeSettings, setThemeSettings] = useState(false);
const [activeMenu, setActiveMenu] = useState(true);
const [isClicked, setIsClicked] = useState(initialState);
const setMode = (e) => {
setCurrentMode(e.target.value);
localStorage.setItem('themeMode', e.target.value);
};
const setColor = (color) => {
setCurrentColor(color);
localStorage.setItem('colorMode', color);
};
const handleClick = (clicked) => setIsClicked({ ...initialState, [clicked]: true });
return (
// eslint-disable-next-line react/jsx-no-constructed-context-values
<StateContext.Provider value={{ currentColor, currentMode, activeMenu, screenSize, setScreenSize, handleClick, isClicked, initialState, setIsClicked, setActiveMenu, setCurrentColor, setCurrentMode, setMode, setColor, themeSettings, setThemeSettings }}>
{children}
</StateContext.Provider>
);
};
export const useStateContext = () => useContext(StateContext);

Laden…
Abbrechen
Speichern