亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我刷新時,React Router 不顯示組件

當我刷新時,React Router 不顯示組件

明月笑刀無情 2023-07-20 15:52:59
我有嵌套路由,第三級路由失敗。這是我的路由結構是的....但問題是,App.js 路由到 -Home -About -Dashboard 然后 Dashboard 有子組件到 -Profile (/dashboard/user) -Account (/dashboard/account) -Wallet (/dashboard/wallet) - 設置 (/dashboard/settings) 然后設置還有其他子組件 - 編輯個人資料 (/dashboard/settings/editprofile) - 編輯密碼和 Pin 圖 (/dashboard/settings/editpassword) - 編輯帳號 (/儀表板/設置/編輯編號)前兩級路由正在工作,但是當我刷新頁面時,最后一個路由失敗,盡管當我返回主頁并單擊鏈接直到到達最后一個組件時它會呈現,但是一旦我刷新瀏覽器,它就會中斷,當我手動輸入時它也不起作用。這是我的 App.js 路由設置import {  BrowserRouter as Router,  Switch,  Route,  Redirect,} from "react-router-dom";const App = () => {  return (    <div className="App">      {/* setting up the routes */}        <Switch>          <Route path="/" exact component={Home} />          <Route path="/dashboard" component={dashboard} />          <Route path="/login" exact component={Login} />          <Route path="/register" exact component={SignUp} />          <Route path="/about" exact component={AboutServices} />        </Switch>      </Router>    </div>  );};我的儀表板.jsimport { Route, useRouteMatch, NavLink, Switch } from "react-router-dom";const App = () => {     let { path, url } = useRouteMatch();  return (    <div className="App">       <nav> Navavigation bar <nav>      {/* setting up the routes */}    <div className="MainBody">      <Switch>        <Route path={`${path}/wallet`} exact component={Wallet} />        <Route path={`${path}/profile`} component={Profile} />        <Route path={`${path}/account`} component={Account} />        <Route path={`${path}/settings`} exact component={Settings} />      </Switch>    </div>     </div>  );};設置頁面import {  Switch,  Route,  useRouteMatch,  NavLink,  BrowserRouter,} 
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

我 99% 確定您的問題是因為您使用了超過 1 個路由器。刪除UIBrowserRouter周圍的內容Settings。我猜測當嵌套路由沒有通過外部路由器的鏈接導航到時,該match道具不會像您期望的那樣初始化。


const Settings = (props) => {

  let { path, url } = useRouteMatch();


  return (

    <div className="Settings">

      <nav>Navigation bar</nav>

      <div className="SettingsWrapper">

        <Switch>

          <Route path={`${path}/editprofile`} component={EditProfile} />

          <Route

            path={`${path}/changepassword`}

            component={ChangePassword}

          />

          <Route path={`${path}/changepin`} component={ChangePin} />

          <Route

            path={`${path}/accountsettings`}

            component={BankSettings}

          />

        </Switch>

      </div>

    </div>

  );

};

編輯

好吧,當刪除嵌套路由器并創建我自己的代碼和盒子時,我發現了嵌套路由的一個小、微妙但重要的“怪癖”。任何渲染更多路由的嵌套路由都不能指定exact路由上的 prop。


const App = () => {

     let { path, url } = useRouteMatch();

  return (

    <div className="App">

       <nav>Navigation bar</nav>


      {/* setting up the routes */}


      <div className="MainBody">

        <Switch>

          ...

          <Route

            path={`${path}/settings`}

            exact // <-- exact match precludes sub-routes!!

            component={Settings}

          />

        </Switch>

      </div> 

    </div>

  );

};

因此,如果路徑是“/dashboard/settings/editprofile”,則路徑不再與路由路徑完全匹配,并且不會呈現路由。


解決方案

exact只需省略嵌套路由渲染子路由的 prop即可。請記住,路由路徑將被視為“前綴”,因此如果沒有exact指定 prop,路徑“/dashboard/settings/editprofile”可以與“/dashboard/settings”匹配。


const Dashboard = () => {

  let { path, url } = useRouteMatch();


  return (

    <div className="App">

      <nav>Dashboard Navigation bar </nav>

      <NavLink to={`${url}/settings`}>Settings</NavLink>


      {/* setting up the routes */}


      <div className="MainBody">

        <Switch>

          ...

          <Route path={`${path}/settings`} component={Settings} /> // <-- no exact match

        </Switch>

      </div>

    </div>

  );

};


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號