【问题标题】:Why browser back button returns a blank page with the previous URL为什么浏览器后退按钮会返回一个带有前一个 URL 的空白页面
【发布时间】:2021-09-27 04:26:17
【问题描述】:

在按下浏览器back button 时,为什么会显示一个空白页面而不是我之前访问过的组件?只有 URL 更改为前一个。
使用 React Router v5

这真是令人沮丧,我该如何解决这个问题?
SignUp.js

 render() {
    return (
      <div className='signUp-div'>
        <Header />
        <Router history={history}>
          <div className='form-div'>
            <Redirect to='/signup/mobile' /> // Default page
            <Switch>
              <Route exact path={'/signup/mobile'} component={MobileNum} />
              <Route exact path={'/signup/idnumber'}>
                <IdentNumber setPersonalID={this.props.setUserNumber} />
              </Route>
              <Route exact path={'/signup/password'}>
                <CreatePass
                  setIfSignUp={this.props.setIfSignUp}
                />
              </Route>
            </Switch>
          </div>
        </Router>
      </div>
    );
  }

IdentNumber.js

const IdentNumber = ({setPersonalID}) => {

  const handleCheckID = () => {
  history.push('/signup/password');
  }
 
  return (
    <div className='form-div'>
      <button              
        onChange={(event) => onChangeHandler(event)}       
      > Page password</button>
    </div>
  );
};

export default IdentNumber;

我解释对了吗?
谢谢

【问题讨论】:

  • 你能把这些组件和导入一起放在一个代码框里吗?然后我们将在聊天中连接。
  • @deechris27 你的意思是你不知道你做了什么改变?我如何追踪它们,我尝试了你在这里告诉我的内容,它在代码沙盒中看起来一样。我怎样才能下载你的文件?我复制了 SigUp 和 Index - 没有改变同样的问题。
  • @deechris27 不是那个 path='/signup/mobile' 让它倒退,它是 exact 键码使它不回到上一页,我不知道为什么.所有这些时间,几天,我认为没有 exact 代码它不会传输页面
  • 因为我改变了很多东西。它何时起作用变得难以追踪。我注释掉了 App 中添加的路线,但仍然有效。是的,您对“确切”的看法是正确的。
  • 除了默认的“/”之外的所有路由都不需要“exact”键,当它包含在switch中并放在最后时。你有像 /signup 和 /signup/mobile 这样的路线,起初让我觉得它是必要的。但完全匹配 /signup 和 /signup/* 相同。 Switch 只渲染一个路由,并且首先匹配哪个路由。路由定义的顺序很重要。这就是让我想到将 MobileNum 路由从注册转移到 App.js 的原因。

标签: reactjs react-router navigation react-navigation react-router-dom


【解决方案1】:

从代码沙盒链接中,我观察到一些可能导致此问题的情况。

import { Router } from "react-router-dom"; 更新您的导入 到 import { BrowserRouter as Router } from "react-router-dom";

&lt;BrowserRouter&gt; 使用 HTML5 历史 API(pushState、replaceState 和 popstate 事件)使您的 UI 与 URL 保持同步。

路线将保持不变。您正在使用 react-router-dom v5.2.0,您可以使用 useHistory 来获取历史对象。 useHistory 简化了组件路由感知的过程。

随着我的变化:https://codesandbox.io/s/suspicious-pine-5uwoq

当它被包含在Switch 中并放置在end 中时,我们不需要exact 键用于除了默认的“/”之外的所有路由。但是exact 匹配/signup/mobile/signup/* 相同。 Switch 只渲染一个路由,并且首先匹配哪个路由

An example project供参考。

如果您想自己处理后退按钮事件,请按照以下示例进行操作。

在函数组件中,我们可以通过监听历史对象来处理后退按钮的按下。

import { useHistory } from 'react-router-dom';

const Test = () => {

  const history = useHistory();
  
  useEffect(() => {
    return () => {
      if (history.action === "POP") {
        
      }
    };
  }, [history])
}

收听useEffect 中的历史记录以了解组件是否已卸载。 history.listen 让我们监听历史的变化。

示例

import { useHistory } from 'react-router-dom';

const Test = () => {
  const history = useHistory();

  useEffect(() => {
    return history.listen(location => {
      if (history.action === 'POP') {
        
      }
    })
  }, [])
}

react-router-dom 现在有Prompt

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

    <Prompt
       message={(location, action) => {
         if (action === 'POP') {
           // back button pressed
         }

       return location.pathname.startsWith("/test")
        ? true
        : `Are you sure you want to go to ${location.pathname}?`
     }}
     />

【讨论】:

  • 那么使用哪一个呢?我想在这些功能中得到什么? onExit(nextState)对不起,我没听懂。
  • 这个路由是从 react-router-dom 还是 react-router 导入的?
  • 另外,尝试使用 withRouter 将您的组件包装在 Route 中。比如
  • component={ withRouter(MobileNum) } 不起作用,没有任何作用。与history.replace() 相同的问题。
猜你喜欢
  • 1970-01-01
  • 2016-07-09
  • 2012-01-25
  • 1970-01-01
  • 2017-10-07
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多