【问题标题】:Blank page on refresh when using nested path React使用嵌套路径 React 时刷新空白页
【发布时间】:2021-08-22 12:50:32
【问题描述】:

当我刷新页面或按下浏览器的前后按钮时,我遇到了这个问题(空白页面)。

我正在使用 Firestore 作为我的后端。

我正在使用:

`"firebase": "^8.2.9"`,
`"react": "^17.0.1"`,
`"react-dom": "^17.0.1"`,
`"react-router-dom": "^5.2.0"`

这是我的路线结构:

ROUTES- Panel(Home route-/) -> Account(nested route-/account) -> verification(nested route-/account/verification)

当我从 Panel.js(/) to Account.js(/account) 导航时,它工作正常。

同样,当我从 Account.js(/account) to verification.js(/account/verification) 导航时,它可以工作。

但是当我refresh the browser 或press the browser's back button or forward button 时,空白页是可见的。

示例:如果我在/account/verification 路由中,现在如果我按下浏览器的后退按钮,我应该导航到/account,但空白页可见

Panel.js:

import React from "react";
import fire from "../../Firebase";
import "./Panel.css";
import logo from "../assets/MMF_White.png";
import { BrowserRouter as Router, Route, Switch, Link } from "react-router-dom";
import Account from "../Accounts/Accounts";
import Notification from "../Notifications/Notifications";

function Panel() {
  return (
    <Router>
      <div className="app-panel">
        <div className="container">
          <div className="logo-div">
            <img className="logo1" src={logo} />
          </div>
          <div className="panel-button-div">
            <button
              className="panel-button"
              onClick={() => fire.auth().signOut()}
            >
              Sign Out
            </button>
          </div>
        </div>

        <div className="header-div">
          <h1>
            Welcome <span className="wel">Admin</span>
          </h1>
        </div>

        <Switch>
          <Route exact path="/account" component={Account} />
          
        </Switch>

        <div>
          <Link to="/account">Account</Link>
         
        </div>
      </div>
    </Router>
  );
}
export default Panel;

Account.js-

import React from "react";
import Verify from "./Verification/Verification";
import { BrowserRouter as Router, Route, Switch, Link } from "react-router-dom";

function Account() {
  return (
    <Router>
      <div>
        <Link to="/account/verification">Verification</Link>
      </div>
      <Switch>
        <Route exact path={`/account/:verification`} component={Verify} />
      </Switch>
    </Router>
  );
}

export default Account;

验证.js:

import React, { useContext, useState } from "react";
import UIVerify from "./CardUIVerify";
import { DataContextVerify } from "../../../contexts/Verification/DataVerify";
import "./Verification.css";

function Verify() {
  // const [user, setUser] = useState("");
  const [showdata1, setShowData1, fetchedItem1] = useContext(DataContextVerify);

  function search1(event) {
    if (event.key === "Enter") {
      fetchedItem1();
    }
  }
  return (
    <div className="verification-div">
      <h2 className="v-header">Verification</h2>
      <input
        className="search-1"
        name="search"
        type="text"
        placeholder="Search.."
        autoComplete="off"
        
        onKeyPress={search1}
      />
      <UIVerify />
    </div>
  );
}

export default Verify;

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore react-router


    【解决方案1】:

    您应该只在应用程序的顶层定义一个 &lt;Router&gt; ... &lt;/Router&gt; 组件。删除 &lt;Router&gt; 并将您的 Account.js 文件中的 path={'/account/:verification'} 更改为 path="/account/verification"

    import React from "react";
    import Verify from "./Verification/Verification";
    import { BrowserRouter as Router, Route, Switch, Link } from "react-router-dom";
    
    function Account() {
      return (
        <div>
          <Link to="/account/verification">Verification</Link>
        </div>
        <Switch>
          <Route path="/account/verification" component={Verify} exact />
        </Switch>
      );
    }
    
    export default Account;
    

    【讨论】:

    • 您好,谢谢您的回复,我尝试了您的解决方案,但仍然显示空白页,现在还出现了一个新问题,即 - 如果我从 / 导航到 /account,空白页是可见的(以前没有发生过)。
    猜你喜欢
    • 2019-09-16
    • 2021-04-15
    • 2020-07-21
    • 1970-01-01
    • 2019-05-31
    • 2017-02-25
    • 1970-01-01
    • 2021-09-23
    • 2020-02-20
    相关资源
    最近更新 更多