0

In my react.js app I have this array in state:

 constructor(props) {
  super(props);

  this.state = {
    userRecord: {
        userName: '',
        userEmail: '',
        userAge: '',
        userHeight: '',
        userRace: '',
        userGender: '',
        createdAt: undefined,
        updatedAt: undefined
      }
   }
 }

then I call a function where I'm trying to set the state of each value

    this.setState({
        userRecord.userName: newuserDocs[0].userName,
        userRecord.userAge: newuserDocs[0].userAge,
        userRecord.userHeight: newuserDocs[0].userHeight,
        userRecord.userRace: newuserDocs[0]. userRace,
        userRecord.userGender: newuserDocs[0].userGender,
        userRecord.userEmail: newuserDocs[0].userEmail,
      });

But I'm getting an error that the period between userRecord and each value is incorrect.

How do I properly set the state of those values?

Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • 2
    `In my react.js app I have this array in state` no you don't have array in state – Code Maniac Apr 07 '20 at 14:13
  • you updating your OBJECT in state - using wrong syntax – developer Apr 07 '20 at 14:18
  • @CodeManiac I meant object,. I have an array too, but I removed it from the example when posting and didn't update my text – Geoff_S Apr 07 '20 at 14:18
  • get object: var userRecord = this.state.userRecord; then update object e.g.: userRecord.userName: newuserDocs[0].userName; then commit state: this.setState ({ userRecord }); – developer Apr 07 '20 at 14:19
  • @developer yes I meant object, I had an array too but I removed it and didn't update my question text when posting. What is the proper syntax here though? – Geoff_S Apr 07 '20 at 14:19
  • 1
    @TomN. no problem :) does the linked question solves your problem. try once if you still face error let us know – Code Maniac Apr 07 '20 at 14:20
  • @CodeManiac thanks! I'll check it now – Geoff_S Apr 07 '20 at 14:20

0 Answers0