1

My store:

{
    people: [
        {
            id: 12345,
            name: Person 1
        },
        {
            id: 54321,
            name: Person 2
        }
    ]
}

I'm trying to update the name property on Person 1. I'm using react-addons-update like this:

update(state, {
    people: {
        [action.person.id]: {
            name: action.person.name
        }
    }
}

Where action.person is an object { id: 12345, name: New name }.

However, I keep getting an error:

Uncaught (in promise) TypeError: Cannot read property 'name' of undefined(…)

This is being thrown in react-addons-update's update method. Specifically,

for (var k in spec) {
    if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {
        nextValue[k] = update(value[k], spec[k]);    // Error occurs here
    }
}

As far as I can tell the problem is that when nextValue, value and spec are all set to people, k is set to action.person.id. And of course that id doesn't exist in the array (it's an array of people).

So my question is, what can I pass in instead of [action.person.id] to do this? According to both this and this what I have should be working.

Community
  • 1
  • 1
RareNCool
  • 426
  • 8
  • 19
  • ps you should be using [immutability-helper](https://github.com/kolodny/immutability-helper) – `react-addons-update` is [deprecated](https://facebook.github.io/react/docs/update.html) – Mulan Oct 28 '16 at 16:20

1 Answers1

2

here you go

update(state, {
    people: {
        $apply: (people) => people.map((person) => {
            if(person.id !== action.person.id) {
                return person;
            }

            return {
                ...person,
                name: action.person.name
            }
        })
    }
}
FlatLander
  • 1,717
  • 15
  • 21