简体   繁体   English

反应路由器 goBack() 无法正常工作

[英]React router goBack() not working properly

I have faced a problem.我遇到了一个问题。 I am used react-router-dom for routing.我使用 react-router-dom 进行路由。 It's working well but goBack is not working properly.它运行良好,但 goBack 无法正常工作。 When I clicked back button it's 1st going to NotFound / Signin page then redirect to back page.当我单击后退按钮时,它首先会转到NotFound / Signin页面,然后重定向到后退页面。 How can I overcome this issue?我怎样才能克服这个问题?

import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import createBrowserHistory from 'history/createBrowserHistory';

import Signin from '../ui/signin/Signin';
import AddEvent from '../ui/events/AddEvent';
import EventView from '../ui/events/EventView';
import NotFound from '../ui/NotFound';

const history = createBrowserHistory();
const privatePages = [
    '/events', 
    '/addevent',

];
const publicPages = ['/', '/signup','/forgotpassword'];

const onEnterPublicPage = () => {
    if (Meteor.userId()) {
        history.replace('/events');
    }
};

const onEnterPrivatePage = () => {
    if (!Meteor.userId()) {
        history.replace('/');
    }
};

export const onAuthenticationChange = (isAuthenticated) => {

    const pathname = this.location.pathname;
    const isUnauthenticatedPage = publicPages.includes(pathname);
    const isAuthenticatedPage = privatePages.includes(pathname);

    if (isAuthenticated && isUnauthenticatedPage) {
        history.replace('/events');
    } else if (!isAuthenticated && isAuthenticatedPage) {
        history.replace('/');
    }
}

export const routes = (
    <Router history = {history}>
        <Switch>
            <Route 
            exact path="/events" 
            component={ListEvents} 
            onEnter={onEnterPrivatePage} />

            <Route 
            exact path="/addevent" 
            component={AddEvent} 
            onEnter={onEnterPrivatePage} />

            <Route  component={NotFound}/>

            <Route 
            exact path="/" 
            component={Signin} 
            onEnter={onEnterPublicPage} />

        </Switch>
    </Router>
);

In the component:在组件中:

constructor(props){
        super(props);
        this.goBack = this.goBack.bind(this);
    }

    goBack(){
        this.props.history.goBack();
        //  this.props.history.push.go(-1);
    }

In the return在返回

<Link
    to="" 
    onClick={this.goBack}
    className="back-icon">
    Back
</Link>

Its because you are using history.replace('/') .这是因为您正在使用history.replace('/') You are replacing, not pushing so there is no previous route.您正在替换,而不是推动,因此没有以前的路线。

One possible way is, Instead of using Link, use history.push to change the route dynamically.一种可能的方法是,不使用 Link,而是使用 history.push 动态更改路由。 To achieve that remove the Link component and define the onClick event on "li" or "button".要实现这一点,请移除 Link 组件并在“li”或“button”上定义 onClick 事件。 Now first perform all the task inside onClick function and at the end use history.push to change the route means to navigate on other page.现在首先执行 onClick 函数内的所有任务,最后使用 history.push 更改路由意味着在其他页面上导航。

I hope this helps我希望这有帮助

I have had the same issue and the history.goBack() function doesn't work with <Link /> component, but if you replace it for any other it will work我遇到了同样的问题, history.goBack() function 不适用于<Link />组件,但如果您将其替换为任何其他组件,它将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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