I have pasted my whole there is very minor problem in this code but I'm not able to resolve the problem. Please review this code.
It's showing following error while consoling:
Uncaught TypeError: Cannot read property 'state' of undefined reactjs
Code:
export class Login extends Component {
constructor(props) {
super(props);
this.handleChangeforSignUp = this.handleChangeforSignUp.bind(this);
this.createNewUserWithEmailPassword = this.createNewUserWithEmailPassword.bind(this);
this.state = {
sign_up_details: {
email: "",
password: "",
confirmpassword: "",
notification: ""
}
};
}
createNewUserWithEmailPassword(event){
event.preventDefault();
if((this.state.sign_up_details.password !== "") && (this.state.sign_up_details.confirmpassword !== "")){
if(this.state.sign_up_details.password === this.state.sign_up_details.confirmpassword){
let updateNotification = Object.assign({}, this.state.sign_up_details, {notification: "Password matched"});
this.setState({
sign_up_details: updateNotification
});
app.auth().createUserWithEmailAndPassword(this.state.sign_up_details.email, this.state.sign_up_details.password)
.catch(function(error) {
// Handle Errors here.
let errorCode = error.code;
console.log(errorCode);
let errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
let notify = Object.assign({}, this.state.sign_up_details, {notification: errorMessage});
this.setState({
sign_up_details: notify
});
} else {
// alert(errorMessage);
let notify = Object.assign({}, this.state.sign_up_details, {notification: errorMessage});
this.setState({
sign_up_details: notify
},
() => {
console.log(this.state.sign_up_details);
} );
}
console.log(error);
}).then(
function(onResolve, onReject){
console.log(onResolve);
console.log(onReject);
}
);
}else{
let notify = Object.assign({}, this.state.sign_up_details, {notification: "Password not matched"});
this.setState({
sign_up_details: notify
});
}
}
}
handleChangeforSignUp(event){
// console.log(event);
const target = event.target;
let emailInput = "";
let passwordInput = "";
let confirmpasswordInput = "";
if(target.type === 'email'){
emailInput = target.value;
let updateEmail = Object.assign({}, this.state.sign_up_details, {email: emailInput});
this.setState({
sign_up_details: updateEmail
});
}
if(target.type === 'password' && target.name === 'password'){
passwordInput = target.value;
let updatePassword = Object.assign({}, this.state.sign_up_details, {password: passwordInput});
this.setState({
sign_up_details: updatePassword
});
}
if(target.type === 'password' && target.name === 'confirmpassword'){
confirmpasswordInput = target.value;
let updateConfirmpassword = Object.assign({}, this.state.sign_up_details, {confirmpassword: confirmpasswordInput});
this.setState({
sign_up_details: updateConfirmpassword
});
}
}
render() {
const { from } = this.props.location.state || { from: { pathname: '/' } }
// const { from } = this.props.history.location || { from: { pathname: '/' } }
if (this.state.redirect === true) {
return <Redirect to={from} />
}
return (
<div id="register" class="container tab-pane fade animated fadeIn">
<form onSubmit={this.createNewUserWithEmailPassword} className="formfield" ref={(form) => { this.signupForm = form }} method="post">
<h1 className="text-center login-heading pt-2 pb-4"><i className="fa fa-pencil fa-lg mx-2" ></i> Register</h1>
{/* <input value={this.state.sign_up_details.name} onChange={this.handleChangeforSignUp} className="form-control input mb-2" name="name" type="text" placeholder="John Doe" /> */}
<input value={this.state.sign_up_details.email} onChange={this.handleChangeforSignUp} className="form-control input mb-2" name="email" type="email" placeholder="[email protected]" />
<input value={this.state.sign_up_details.password} onChange={this.handleChangeforSignUp} className="form-control input mb-2" name="password" type="password" placeholder="*******" />
<input value={this.state.sign_up_details.confirmpassword} onChange={this.handleChangeforSignUp} className="form-control input mb-2" name="confirmpassword" type="password" placeholder="*******" />
<p className="text_ter text_pink">{this.state.sign_up_details.notification !== "" ? this.state.sign_up_details.notification : ""}</p>
<input type="submit" className="btn btn-primary btn-sm btn-block p-2 mt-4 mb-2" value="Log In"></input>
</form>
</div>
)
}
}
ReactDOM.render(<Login/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.min.js"></script>
<div id="app"><div>
thisare bound to the class.catch, yourthisdoes not refer to the component class, so it does not have any state. You should declare it outside the async request, something likelet self = this.stateand then use it.else { // alert(errorMessage); let notify = Object.assign({}, this.state.sign_up_details, {notification: errorMessage}); this.setState({ sign_up_details: notify }, () => { console.log(this.state.sign_up_details); } );in this block of code