0

I am trying to update my object using this.setState({dob.year: '1980'}) but this code doesn't work.

getInitialState: function() {
  return {
    dob: {
      'month' : 'January',
      'day' : '01',
      'year' : '2015'
  };
},
Henrik Andersson
  • 41,844
  • 15
  • 95
  • 89
kyrilkin
  • 193
  • 1
  • 1
  • 9

1 Answers1

2

setState has no magic in it for expanding paths like dob.year. It just takes normal javascript objects. If you want to update a sub-state property without touching the rest of the object, you have to do it manually.

this.setState({dob: Object.assign({}, this.state.dob, { year: '1980'} })
Kyeotic
  • 19,470
  • 10
  • 67
  • 126