Last active
February 3, 2019 03:08
-
-
Save trujic1000/ad2da85265510a14375d34d65a1bc47b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Actual Code | |
constructor() { | |
super(); | |
this.state = { | |
name: "", | |
email: "", | |
password: "", | |
password2: "", | |
errors: {} | |
}; | |
} | |
// onChange method to link fields with state | |
onChange = e => { | |
this.setState({ [e.target.name]: e.target.value }); | |
}; | |
// Submitting the form | |
onSubmit = e => { | |
e.preventDefault(); | |
const newUser = { | |
name: this.state.name, | |
email: this.state.email, | |
password: this.state.password, | |
password2: this.state.password2 | |
}; | |
}; | |
// What I want | |
constructor() { | |
super(); | |
this.state = { | |
user: { | |
name: "", | |
email: "", | |
password: "", | |
password2: "" | |
}, | |
errors: {} | |
}; | |
} | |
// onChange method to link fields with state | |
onChange = e => { | |
this.setState({ user[e.target.name]: e.target.value }); // This is what I want to do - this fails | |
}; | |
// Submitting the form | |
onSubmit = e => { | |
e.preventDefault(); | |
// All of that so I can use spread operator instead of typing it | |
const newUser = { | |
...this.state.user | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment