Skip to content

Commit

Permalink
refactor deprecated ComponentWillReceiveProps
Browse files Browse the repository at this point in the history
  • Loading branch information
pjblitz86 committed Jan 17, 2019
1 parent b25124b commit bdf64cc
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions client/src/components/auth/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,36 @@ class Register extends Component {
}
}

componentWillReceiveProps(nextProps) {
if (nextProps.errors) {
this.setState({ errors: nextProps.errors });
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.errors !== prevState.errors) {
return { errors: nextProps.errors };
} else return null;
}

componentDidUpdate(prevProps, prevState) {
if (prevProps.errors !== prevState.errors) {
this.setState({ errors: prevProps.errors });
}
}

onChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
}

onSubmit = (e) => {
e.preventDefault();
const { name, email, password, password2 } = this.state;
const newUser = {
name: this.state.name,
email: this.state.email,
password: this.state.password,
password2: this.state.password2
name,
email,
password,
password2
}
this.props.registerUser(newUser, this.props.history);

}

render() {
const { errors } = this.state;
const { errors, name, email, password, password2 } = this.state;
return (
<div className="register">
<div className="container">
Expand All @@ -57,15 +64,15 @@ class Register extends Component {
<TextFieldGroup
placeholder="Name"
name="name"
value={this.state.name}
value={name}
onChange={this.onChange}
error={errors.name}
/>
<TextFieldGroup
placeholder="Email"
name="email"
type="email"
value={this.state.email}
value={email}
onChange={this.onChange}
error={errors.email}
info="This site uses Gravatar so if you want a profile image, use a Gravatar email"
Expand All @@ -74,15 +81,15 @@ class Register extends Component {
placeholder="Password"
name="password"
type="password"
value={this.state.password}
value={password}
onChange={this.onChange}
error={errors.password}
/>
<TextFieldGroup
placeholder="Confirm Password"
name="password2"
type="password"
value={this.state.password2}
value={password2}
onChange={this.onChange}
error={errors.password2}
/>
Expand Down

0 comments on commit bdf64cc

Please sign in to comment.