Skip to content

Instantly share code, notes, and snippets.

@trujic1000
Last active February 3, 2019 03:08
Show Gist options
  • Save trujic1000/ad2da85265510a14375d34d65a1bc47b to your computer and use it in GitHub Desktop.
Save trujic1000/ad2da85265510a14375d34d65a1bc47b to your computer and use it in GitHub Desktop.
// 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