1

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?

2
  • 1
    The result of the Promise doesn't look to be used anywhere. If the catch occurs, it's just an orphaned expression not used anywhere.? Commented Jan 18, 2019 at 22:28
  • If the catch occurs, when I write catch(e => console.log(e)) . I can read the error message in console. What can I do to render the message? Commented Jan 18, 2019 at 22:31

2 Answers 2

2

your solution is correct, you can also delegate the handleSubmit to the parent component, then if you have an error you display the Snackbar component otherwise you display the Form component.

In your example, if you don't want to post the error, you can destructure the state object to control the properties you want to send.

handleSubmit = (event) => {
  const {email, password} = this.state;
  event.preventDefault()
  //api call here

  onFormSubmit({email, password})
    .then(() => history.push('/teams'))
    .catch(e => this.setState({error: e})
}
Sign up to request clarification or add additional context in comments.

Comments

1

You will need to place that component into rendered JSX, or inject it somehow. Are you calling handleSubmit inside of your render's return?

One option is to use JQuery inside of that catch to inject that Snackbar onto the page.

A better option is to somehow trigger that component inside of your render's return like normal. For instance, set a localState variable of error to true inside of the catch, and then render the snackbar conditionally inside of your render based off this.state.error being true.

4 Comments

I call handleSubmit when click submit button
I updated my post with your idea. I just wonder, is there a cleaner way to have thing done.
I don't believe there is any way to inject that component to the current page solely within a non-render function without using something like JQuery.
If this answer was a solution to your problem, I'd love to get your vote on it! Just starting out on here, and it'd be nice to get some reputation points. If you even get those for having the chosen answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.