Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda