Edit a property in an array of objects in React based on an ID

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Edit a property in an array of objects in React based on an ID



I have an array of objects created in the new "Context API" like this ...


const reducer = (state, action) =>
switch (action.type)
case "DELETE_CONTACT":
return
...state,
contacts: state.contacts.filter(contact =>
return contact.id !== action.payload;
)
;
default:
return state;

;

export class Provider extends Component
state =
contacts: [

id: 1,
name: "John Doe",
email: "jhon.doe@site.com",
phone: "01027007024",
show: false
,

id: 2,
name: "Adam Smith",
email: "adam.smith@site.com",
phone: "01027007024",
show: false
,

id: 3,
name: "Mohammed Salah",
email: "mohammed.salah@site.com",
phone: "01027007024",
show: false

],
dispatch: action =>
this.setState(state => reducer(state, action));

;

render()
return (
<Context.Provider value=this.state>
this.props.children
</Context.Provider>
);




I want to create an action in the "reducer" that allows me to edit each contact's "show" property based on its id that I will pass to the action as a payload, how can I do that?




2 Answers
2



To avoid array mutation and retain the element position while editing contact you can do this:


case "EDIT_CONTACT":
const id, show = action.payload;
const contact = ...state.contacts.find(c => c.id === id), show ;
return
...state,
contacts: state.contacts.map(c => return (c.id !== id) ? c : contact;)
;





Thanks, man. It worked like a charm :)
– Ruby
Aug 11 at 10:12



You can find the contact, avoid mutation by using spread, set new value of show :


spread


show


case "EDIT_CONTACT":
const id, show = action.payload; // Assume id and show are in action.payload
const contact = ...state.contacts.find(c => c.id === id), show ;
return
...state,
contacts: [...state.contacts.filter(c => c.id !== id), contact]
;



If order matters:


const id, show = action.payload;
const contact = ...state.contacts.find(c => c.id === id), show ;
const index = state.contacts.findIndex(c => c.id === id);
return
...state,
contacts = [ ...state.contacts.slice(0, index), contact, ...state.contacts.slice(index + 1)];





Well, that works, but after it edits the property it pushes the edited object to the bottom of the array.
– Ruby
Aug 10 at 13:22






And if I dot it like this ... contacts: [contact, ...state.contacts.filter(c => c.id !== id)] it puts it at the top of the array.
– Ruby
Aug 10 at 13:25



contacts: [contact, ...state.contacts.filter(c => c.id !== id)]





How to edit it and keep it in the same place?
– Ruby
Aug 10 at 16:23





@Ruby I've edited the answer
– Faly
Aug 11 at 3:11





Thanks for the solution, it works, but I think @samee solution looks more elegant.
– Ruby
Aug 11 at 10:11






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard