Navigating Programmatically in React-Router v4

后端 未结 10 1141
后悔当初
后悔当初 2020-12-02 11:16

I couldn\'t wait and I jumped into using the latest alpha version of react-router v4. The all-new is great in keeping your U

10条回答
  •  眼角桃花
    2020-12-02 12:13

    Using withRouter will add router properties to you component, then you can access the history and use push like you did with v3:

    import React from 'react';
    import { withRouter } from 'react-router-dom';
    
    class Form extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          input: '',
        };
    
        this._submit = this._submit.bind(this);
      }
    
      render() {
        return (
          
    this.setState({input: event.target.value})}/>
    ); } _submit(event) { event.preventDefault(); this.props.history.push(`/theUrlYouWantToGoTo`); } } export default withRouter(Form);

提交回复
热议问题