Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
How to increase/decrease value with reducer?

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());
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue is a mismatch between how you've defined your state

const initialStateValue = {
  value: 20
};

and how you're using it:

increaseTemp: (state = initialStateValue) => {
  state = state + 1;
},

If state is an object that has a value property then your reducer should be returning a new state that has the updated value property.

e.g.

increaseTemp: (state = initialStateValue) => {
  const { value } = state;
  return { ...state, value: value+1 }
},
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda