Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!