Vipin Dubey
Vipin Dubey

Reputation: 592

How to change/update value in store in redux in react -native

i am using redux to store the user data like his name,user id ,user image and user email in redux. The reducer for that is

const initialState = {
  user: []
};
function rootReducer(state = initialState, action) {

  if (action.type === 'ADD_USER') {
    return Object.assign({}, state, {
      user: state.user.concat(action.payload)
    });

  }
  if (action.type === 'REMOVE_USER') {
    //return Object.assign({}, initialState)
    return {
      ...state,
      user : []
    }

  }
  if (action.type === 'CHANGE_USER') {

     state.user.splice(0)

    return Object.assign({}, state, {
      user: state.user.concat(action.payload)
    });

  }


  return state;
};
export default rootReducer;

To add user i am dispatching like this

 let user_id = response[0][1]
          let user_name = response[1][1]
          let user_image = response[2][1]
          let email = response[3][1]

            this.props.add({ user_id, user_name, user_image, email });

Suppose now that i want to change the email or name of the user so how can i change/update the specific field like email ? Currently i am doing like this

this.props.change({ user_id : 1, user_name : 'sdfd', user_image:'', email:'[email protected]' });

This passes an action CHNAGE_USER After above line i am calling

let a = JSON.stringify(this.props.user)
  console.log("val-----------",a)

I mean after updating data i am trying to get latest but it gives me empty array

val----------- []

Please tell me both the way to change whole object and to change specific field like email ? Thanks is advance !

Upvotes: 0

Views: 7422

Answers (2)

lavor
lavor

Reputation: 1877

In the redux documentation you can find recipes for inserting/removing/updateing item in an state array.

Upvotes: 1

AlainIb
AlainIb

Reputation: 4708

!!! this is an example for storing data of ONLY ONE USER in redux and this don't work if you have many users to store !!!

    const initialState = {
        user: {  }
    };
    function rootReducer(state = initialState, action) {
        switch (action.type) {
            case "ADD_USER":
                return {
                    ...state,
                    user: action.payload
                };
            case "REMOVE_USER":
                return {
                    ...state,
                    user: {}
                };

            case "UPDATE_USER":
                /* so you return a new state object with all the data from old state
                user also contain the data from old state.user but you update some of his parameters
                like this.props.updateUser({  email:'[email protected]' }); update only email field 
                this.props.updateUser({   user_name : 'sdfd' , email:'[email protected]' }); update only user_name field

                change the name of action too, because later you will add more item in the redux and "change" dont say it change what ( just an idea )
                */
                return {
                    ...state,
                    user: {
                    ...state.user,
                    ...action.payload
                    }
                };

            default: return state;
        }
    }
    export default rootReducer;

Upvotes: 1

Related Questions