简体   繁体   中英

How can i update a name field from single element in array of object from a state in reducer in React-Redux?

Below are my initial state of reducers:

const InitialState = {
    data:[],
    SelectUser : null,
    name: "Not Yet Selected"
};

Below is my data[] state which is fetched from api:

[
 {
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
 },
 {
  "id": 2,
  "name": "Ervin Howell",
  "username": "Antonette",
  "email": "Shanna@melissa.tv",
  "address": {
    "street": "Victor Plains",
    "suite": "Suite 879",
    "city": "Wisokyburgh",
    "zipcode": "90566-7771",
    "geo": {
      "lat": "-43.9509",
      "lng": "-34.4618"
    }
  },
  "phone": "010-692-6593 x09125",
  "website": "anastasia.net",
  "company": {
    "name": "Deckow-Crist",
    "catchPhrase": "Proactive didactic contingency",
    "bs": "synergize scalable supply-chains"
  }
 }
]

I want to change name from a object in state data[] to state name . Where id of object is same as that of id in object of SelecUser state. SelectUser is also object whose contents are same as that of element in data[] . All state are already set.

Below are reducers created until now:


const InitialState = {
    data:[],
    SelectUser : null,
    name: "Not Yet Selected"
};

export default (state=InitialState, action)=>{
    switch(action.type){
            case 'FETCH_USER':
                return {...state,data:action.payload};
            case 'USER_SELECTED':
                return {...state,SelectUser:action.payload};
            case 'USER_CHANGE':
                return {...state,name:action.payload};
            default:
                return state;
    }
};

All mentioned cases are previously executed.

I want to change name from a object in state data[] to state name in object with a same id which is already stored in SelectUser

I'm assuming that you want to change the name uppon an action which for simplicity I'm calling CHANGE_NAME .

case 'CHANGE_NAME' return handleNameChange(state, action)

Now you just need to find in data the correspondent object, spread it's original content and override name

const handlerNameChange = (state, action) =>{
    const { name } = action

    return {
        ...state,
        data : state.data.map(item =>{
            if(item.id !== state.SelectUser.id) return item
            return {
                ...item,
                name
            }
        })
    }
}

Try this,

export default (state=InitialState, action)=>{
    switch(action.type){
            case 'FETCH_USER':
                return {...state,data:action.payload};
            case 'USER_SELECTED':
                return {...state,SelectUser:action.payload};
            case 'USER_CHANGE':
                return {
                         ...state,
                         name: state.data.reduce((prev, item)=>{
                                  if(state.SelectUser && state.SelectUser.some(elt=>elt.id === item.id)){
                                      return item.name;
                                  }else{
                                      return prev;
                                  }
                         }, null);
                };
            default:
                return state;
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM