React Router Redirect Conditional

后端 未结 3 835
生来不讨喜
生来不讨喜 2021-02-10 19:10

I\'m trying to make a button that only redirects the user to a new page after validation is completed correctly.

Is there a way of doing something like this? How to I g

3条回答
  •  囚心锁ツ
    2021-02-10 19:38

    Like Purgatory said you can do it without using , but otherwise you could make your component a stateful component and then do a conditional render like so

    render() {
        !this.state.redirect ? 
           :
          
      }
    

    And let the saveAndContinue() change the component state.

    saveAndContinue () {
        var valid = validator.validate(this.props.form)
        if (valid) {
          axios.post('/path')
          this.setState({redirect: true});
        } else {
          validator.showErrors()
        }
      }
    

    When the state changes it would cause a re-render and this time the would be rendered.

    Note: I didn't actually run this code snippet, so it may contain (hopefully minor) errors.

提交回复
热议问题