Skip to content
the poor coder

react redux how to change object value in reducer

Poor Man
Published
Reading time
2 min read
On this page

React Redux: How to Change Object Value in Reducer#

As a web developer, I have worked on several projects that used React and Redux together. One common task that I have come across is changing the value of an object in the reducer. In this blog post, I will explain how to do that in detail.

Understanding Reducers#

Reducers are pure functions that take the current state and an action as arguments and return a new state. They are responsible for updating the state of the application. In Redux, the state is usually an object or an array.

Changing Object Value in Reducer#

Let's say we have an object in our state called 'user' which looks like this:

Text
const initialState = {
    user: {
        name: 'John',
        age: 25,
        email: 'john@example.com'
    }
}

Now, let's say we want to change the age of the user to 30. We can achieve this by creating a new object with the updated value and return it from the reducer. Here's how we can do that:

Text
const reducer = (state = initialState, action) => {
    switch (action.type) {
        case 'CHANGE_AGE':
            return {
                ...state,
                user: {
                    ...state.user,
                    age: 30
                }
            }
        default:
            return state
    }
}

In the above code, we have created a new object using the spread operator (...) and updated the age property in it. We then return this new object from the reducer.

We also need to dispatch an action to trigger this change in the reducer. Here's an example:

Text
const changeAge = () => {
    return {
        type: 'CHANGE_AGE'
    }
}

dispatch(changeAge())

In the above code, we have created a function called 'changeAge' which returns an object with the type 'CHANGE_AGE'. We then dispatch this action using the 'dispatch' function.

Other Ways to Change Object Value in Reducer#

There are several other ways to change the value of an object in the reducer. One way is to use the 'Object.assign' method. Here's how we can do that:

Text
const reducer = (state = initialState, action) => {
    switch (action.type) {
        case 'CHANGE_AGE':
            return Object.assign({}, state, {
                user: Object.assign({}, state.user, {
                    age: 30
                })
            })
        default:
            return state
    }
}

In the above code, we have used the 'Object.assign' method to create a new object with the updated value of age. We then return this new object from the reducer.

Another way is to use a library like Immer which provides a simple and efficient way to update immutable objects. Here's an example:

Text
import produce from 'immer'

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case 'CHANGE_AGE':
            return produce(state, draftState => {
                draftState.user.age = 30
            })
        default:
            return state
    }
}

In the above code, we have used the 'produce' function from Immer to update the age property of the user object. We then return the updated state from the reducer.

Conclusion#

Changing the value of an object in the reducer is a common task in Redux. We can achieve this by creating a new object with the updated value and returning it from the reducer. There are several other ways to do this, such as using the 'Object.assign' method or a library like Immer.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC