0

For example,

state = {
  data: {}
}

How can I add a new nested field into an object? I cannot set that field, because have an error Cannot read property 'date' of undefined

const reducer = produce((draft, action) => {
   switch (action.type) {
      case 'ACTION_SUCCESS':
      draft.data.children.date = action.response;
   }
});

As a result I want:

  data: {
     children: {
        data: 'date'
     }
  }
}
Liudmyla
  • 1
  • 2

1 Answers1

1

Normal JS object manipulation rules apply here. You can't write obj.x.y.z = if there is no .y field yet - you have to create that first.

markerikson
  • 63,178
  • 10
  • 141
  • 157