简体   繁体   中英

How add new field into reducer using Immer.js?

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'
     }
  }
}

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

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