2

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?

Ruby
  • 2,207
  • 12
  • 42
  • 71

2 Answers2

1

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;})        
    };
samee
  • 537
  • 2
  • 9
0

You can find the contact, avoid mutation by using spread, set new value of 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)];
    }
Faly
  • 13,291
  • 2
  • 19
  • 37
  • Well, that works, but after it edits the property it pushes the edited object to the bottom of the array. – Ruby Aug 10 '18 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 '18 at 13:25
  • Thanks for the solution, it works, but I think @samee solution looks more elegant. – Ruby Aug 11 '18 at 10:11