I'm trying to have my app redirect to 'home' once a user has logged in. I do have an onSubmitSignin function that evaluates the log in credentials(from a temporary on page database) and then runs onRouteChange function which is then supposed to change the route to 'home' but instead I get the error "this.setState is not a function".
New to react, there's definitely something I don't get here. Any insight would be really appreciated, thanks!
App.js code:
const initialState = {
route: 'signin',
isSignedIn: false
}
class App extends Component {
constructor(props){
super(props);
this.state = initialState;
}
onRouteChange (route) {
if (route === 'signout') {
this.setState({initialState});
} else if (route === 'home') {
this.setState({isSignedIn: true})
}
this.setState({route: route})
}
render () {
const { isSignedIn } = this.state;
return (
<div className="App">
<Navigation onRouteChange={this.onRouteChange} />
{isSignedIn ? <Account /> : <Signin onRouteChange={this.onRouteChange}/> }
</div>
);
}
}
export default App;
Signin code:
et database = {
email: 'test@gmail.com',
password: 'test'
}
class Signin extends React.Component {
constructor(props){
super(props);
this.state = {
signInEmail: '',
signInPassword: ''
}
}
onEmailChange = (event) => {
this.setState({signInEmail: event.target.value})
}
onPasswordChange = (event) => {
this.setState({signInPassword: event.target.value})
}
onSubmitSignIn = (e) => {
e.preventDefault();
if (this.state.signInEmail === database.email && this.state.signInPassword === database.password) {
this.props.onRouteChange('home');
} else {
return (this.props.onRouteChange('signout'));
}
}
render () {
return (
<div className="form">
<form>
<div className="form-group">
<label htmlFor="email">Email address</label>
<input onChange ={this.onEmailChange} type="email" className="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email"/>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input onChange ={this.onPasswordChange} type="password" className="form-control" id="password" placeholder="Password"/>
</div>
<div className="signInButton">
<button onClick={this.onSubmitSignIn} type="submit" className="btn btn-secondary">Sign in</button>
</div>
</form>
</div>
)
}
}
export default Signin;