Home > Enterprise >  How to increase/decrease value with reducer?
How to increase/decrease value with reducer?

Time:05-12

I am working on a temperature control app with react native. To get the value of the temperature I use redux toolkit. My problem is that my code for increasing/decreasing the initial value with the reducers doesn't work. I get the 20 as value but using handlers to dispatch(in/decreaseTemp()) doesn't do anything. What am I doing wrong?

Reducers:

    import { createSlice } from '@reduxjs/toolkit';

    const initialStateValue = {
        value: 20
    }

    const tempSlice = createSlice({
        name: 'temp',
        initialState: initialStateValue,
        reducers: {
            increaseTemp: (state = initialStateValue) => {
               state = state   1;
            },
            decreaseTemp: (state = initialStateValue) => {
               state = state - 1;
            }
        }
    });

    export const {increaseTemp, decreaseTemp} = tempSlice.actions;
    export default tempSlice.reducer;

The handlers:

    function increaseTempHandler() {
      dispatch(increaseTemp());
    }

    function decreaseTempHandler() {
      dispatch(decreaseTemp());
    }

CodePudding user response:

I suppose it is using immer and you must mutate it:

instead of this:

       state = state   1;

do

       state.value  ;
  • Related