Here is my code, I am using React: I am using Local Storage in order to persist the state even after page refresh and to remain on the same page.
class App extends React.Component{
constructor(props){
super(props);
this.state = localStorage.getItem("app_state") ? localStorage.getItem("app_state") : {
account: {
email: "",
password: ""
},
errorMessage: "",
token: "",
authenticated: false
};
}
notLoggedInhomePage = () =>{
if(!this.state.authenticated) {
return (
<form onSubmit={this.handleSubmit}>
<br/>
<label>Email Address</label>
<input type="text" value={this.state.account.email} onChange={this.handleChange} name="email"/>
<label>Password</label>
<input type="password" value={this.state.account.password} onChange={this.handleChange} name="password" />
<div>
{this.state.errorMessage}</div>
<br/>
<Button type="submit" onClick={() => {
this.someFunction(String(this.state.account.email));
}}>Sign In</Button>
</form>
);
}else{
return(<Redirect to="/items/somelink" />);
}
}
componentDidUpdate(prevProps, prevState){
if(this.state.token !== prevState.token){ //set a new state if token changes
localStorage.setItem("app_state", this.state);
}
}
}
export default App;
Here is the error that I am getting:
It is saying that the email is undefined, what is the reason behind such error message, and why/how is the email undefined, even though it's defined as an empty string in the state.
What is a possible fix to the above ?