在这个项目中,我使用了反应钩子,这个片段用于更改项目的颜色主题,但有一个问题我无法解决。 常量光主题 = { ... }
const darkTheme = {
...
}
export const ThemeState = ({children}) => {
const initialState = {
theme: lightTheme
}
const [state, dispatch] = useReducer(ActionReducer, initialState)
const {theme} = state
const themeToggler = (e) => {
e.preventDefault()
console.log(theme)
if(theme == lightTheme){
dispatch({type:THEME, payload: darkTheme})
}
else if(theme == darkTheme){
dispatch({type:THEME, payload: lightTheme})
}
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
我的减速器:
export const ActionReducer = (state, action) => {
switch(action.type) {
case THEME:
return{
...state,
theme: action.payload
}
default:
return state
}
};
Here is component with toggle button by each click, I need to change theme in state, it clicks correctly:
import {ThemeContext} from '../../store/themeState'
function Main () {
const {theme, themeToggler} = useContext(ThemeContext)
return (
<button onClick={e => {themeToggler(e)}}></button>
)
}
export default Main
when I press the button i catch this log
({body: "#E2E2E2", text: "#363537"}
{body: "#363537", text: "#FAFAFA"}
{body: "#363537", text: "#FAFAFA"}
....)
I don't know why do state changes like this. If you can, help me to solve this bug.)
最初的主题是body: '{#E2E2E2', text: '#363537'}
。
单击该按钮时,将记录此主题{body: "#E2E2E2", text: "#363537"}
,这是正确的初始主题。
记录后,您将主题更新为深色主题{body: '#363537', text: '#FAFAFA'}
。
一旦您再次单击按钮,就会记录此内容:{body: "#363537", text: "#FAFAFA"}
.
现在问题来了,由于您在每次渲染上都创建了 darkTheme 对象,因此引用与以前的引用不同,因此比较else if(theme == darkTheme)
失败,因为 darkTheme 对象与之前的 darkTheme 对象不同。
将主题对象生成移出组件,这样就不会在每次渲染时生成新对象,或者向主题添加类型:
const lightTheme = {
body: '#E2E2E2',
text: '#363537',
type: "light"
}`
并比较这些:if(theme.type == darkTheme.type)
您需要在单击按钮时切换主题:
所以你可以这样做:
主题状态
import React, { useReducer } from "react";
import { TOGGLE_THEME } from "./ActionTypes";
import ActionReducer from "./ActionReducer";
const lightTheme = {
body: "#E2E2E2",
text: "#363537"
};
const darkTheme = {
body: "#363537",
text: "#FAFAFA"
};
const initialState = {
isLightTheme: true
};
export const ThemeState = ({ children }) => {
const [state, dispatch] = useReducer(ActionReducer, initialState);
const { isLightTheme } = state;
const themeToggler = e => {
e.preventDefault();
dispatch({ type: TOGGLE_THEME });
};
const backgroundColor = isLightTheme ? lightTheme.body : darkTheme.body;
const fontColor = isLightTheme ? lightTheme.text : darkTheme.text;
return (
<div style={{ backgroundColor: `${backgroundColor}` }}>
<p style={{ color: `${fontColor}` }}>Some Text</p>
<button type="submit" onClick={themeToggler}>
Toggle Theme
</button>
<hr />
Current Theme: {isLightTheme ? "light" : "dark"}
</div>
);
};
动作减速器:
import { TOGGLE_THEME } from "./ActionTypes";
export default function(state, action) {
switch (action.type) {
case TOGGLE_THEME:
return {
...state,
isLightTheme: !state.isLightTheme
};
default:
return state;
}
}
操作类型:
export const TOGGLE_THEME = "TOGGLE_THEME";
代码沙盒
我创建了一个关于如何实现此行为的最小示例:
const darkTheme = {
color: "green"
};
const lightTheme = {
color: "red"
};
export default function App() {
const [useLightTheme, setUseLightTheme] = useState(true);
return (
<div className={useLightTheme ? darkTheme.color : lightTheme.color}>
<button onClick={() => setUseLightTheme(!useLightTheme)}>toggle</button>
some example text
</div>
);
}
由于您没有提供所有相关代码(例如化简器(,因此我无法解释您的代码。