![](/img/trans.png)
[英]React with Hooks: how to correctly update state after a click event?
[英]React hooks, state is not changing correctly
在这个项目中,我使用了 React 钩子,这个片段用于改变项目的颜色主题,但有一个我无法解决的问题。 const lightTheme = { ... }
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 sat 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>
);
}
由于您没有提供所有相关代码(例如减速器),我无法解释您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.