How to push to History in React Router v4?

后端 未结 21 2168
不思量自难忘°
不思量自难忘° 2020-11-22 00:27

In the current version of React Router (v3) I can accept a server response and use browserHistory.push to go to the appropriate response page. However, this isn

21条回答
  •  一个人的身影
    2020-11-22 01:12

    so the way I do it is: - instead of redirecting using history.push, I just use Redirect component from react-router-dom When using this component you can just pass push=true, and it will take care of the rest

    import * as React from 'react';
    import { Redirect } from 'react-router-dom';
    class Example extends React.Component {
      componentDidMount() {
        this.setState({
          redirectTo: '/test/path'
        });
      }
    
      render() {
        const { redirectTo } = this.state;
    
        return 
      }
    }
    

提交回复
热议问题