I’ve tried to do an example from the Redux Toolkit website and it simply doesnt work. I can see an action, but value isn
t changing. I’m new to RTK. Can you help me?
This is code from react page
import { useDispatch } from "react-redux";
import {
increment,
decrement,
incrementByAmount,
} from "../redux/counter/counterSlice";
const About = () => {
const dispatch = useDispatch();
const handleIncrement = () => {
dispatch(increment());
};
const handleDecrement = () => {
dispatch(decrement());
};
const handleByAmount = (amount) => {
dispatch(incrementByAmount(amount));
};
return (
<>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
<button
onClick={() => {
handleByAmount(5);
}}
>
Increment By Amount
</button>
</>
);
};
export default About;
This is code from slice
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
value: 0,
};
export const counterSlice = createSlice({
name: "counter",
initialState,
reducers: {
increment: (state) => {
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
export default counterSlice.reducer;
This is from store
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "../redux/counter/counterSlice";
export const store = configureStore({
reducer: {
counter: counterReducer,
},
});
I’ve started this code and observed it from devtools.
And again, I can see an action, but value isn’t changing
New contributor
Maksym is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.