L
L
LazariusPaskalius2020-04-25 08:20:21
JavaScript
LazariusPaskalius, 2020-04-25 08:20:21

How to cancel the redirect to the main page?

It is necessary that after submitting the form, if the function responds negatively, no redirect occurs, and if it is positive, it does. By default, a redirect always occurs. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-25
@hzzzzl

import { Redirect } from "react-router-dom"

......

state = { shouldRedirect: false, error: null }

onSubmit = () => {
  const response = /* ..... true | false */ 
  this.setState({ 
    shouldRedirect: response,
    error: response ? null : 'Something wrong!'
  })
}

render() {
  if (this.state.shouldRedirect) return <Redirect to="/" />

  return (
    { this.state.error && <div className="error">{this.state.error}</div> }
    .......
  )
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question