I have been working with react for a while but I got to very basic problem but I don't understand why it doesn't work. Basically, in Login component I submit a form and when it return an error, render a snackbar message.
handleSubmit = (event) => {
event.preventDefault()
//api call here
onFormSubmit(this.state)
.then(() => history.push('/teams'))
.catch(e => <Snackbar message={e}/>)
}
render() {
Console shows no errors but I don't see any snackbar when the onFormSubmit call fails.
Update
I understand now, I didn't place Snackbar inside render () { return (.... I can think of the only way to do it like that:
this.state = {
email: '',
password: '',
error: ''
}
handleSubmit = (event) => {
event.preventDefault()
//api call here
onFormSubmit(this.state)
.then(() => history.push('/teams'))
.catch(e => this.setState({error: e})
}
render () {
return (
<div>
{this.state.error && <Snackbar message={this.state.error}/>}....
But I don't want to submit also the error field to server. Is there another way to inject it to component?
catchoccurs, it's just an orphaned expression not used anywhere.?