In this project I use react hooks, this snippet used to change color theme of project, but there's the problem which I can not to solve. 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 sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
My reducer:
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.)