简体   繁体   中英

Redirect not working, it doesn't redirect to page

I have problem with Redirect , it doesn't work in my code. It stays on the same page without redirection.

Can you tell me why?


Source code (without doesn't matter code):

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

class Test extends Component {

    onError = err => {
        if (err.response && err.response.status === 500) {
            return <Redirect to={{
                pathname: '/',
                state: {error: err}
            }}/>;
        }
    }
}

My code is ugly. The main idea is to wrap Test class to withRouter which pass route props to class Test.

 import React from 'react'; import { withRouter } from 'react-router-dom'; function Test = () => withRouter( class Test extends React.Component { onError = (error) => { const { response } = error; if (response && response.status === 500) { const { history } = this.props; history.replace('/', { error }); } } render() { return null; } } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM