R
R
reactreact2015-12-03 19:28:05
JavaScript
reactreact, 2015-12-03 19:28:05

How to add data to an object (react.js)?

There is an object

getInitialState: function() {
  return {
    dob: {
      'month' : 'January',
      'day' : '01',
      'year' : '2015'

  };
},

How to change the data in it, for example, for year?
It doesn't work this.setState({dob.year: '1980'})

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Antropov, 2015-12-03
@reactreact

for example

this.setState({
    dob: {
         ...this.state.dob,
         year: '1980'
    }
})

Although this can be optimized for example using ramdajs.com/0.18.0/docs/#lens

N
Nikita Gushchin, 2015-12-03
@iNikNik

this.setState({ dob: { year: '1980' }});

A
Alexander, 2015-12-04
@OneFive

It's also possible like this:

this.state.dob.year = 1980;
this.setState(this.state);
// или так 
this.setState(function(state) {
    state.dob.year = 1980;
    return state;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question