为了账号安全,请及时绑定邮箱和手机立即绑定

React Router 在使用 <Link> 时显示空白页面

React Router 在使用 <Link> 时显示空白页面

慕娘9325324 2022-12-22 10:07:16
在更新了我们的一堆依赖项之后,我们现在被迫使用导致重定向的锚标记,在我们使用之前但现在每次我们点击它都会转到正确的 url 即 /screens 但它只是一个空白页面并且从不显示组件,下面是我们的父 index.tsx 文件:ReactDOM.render(  <Provider {...stores}>    {/* Changed routes to children */}    {/* <Router children={routes} history={createHistory} /> */}    <Router history={createHistory}>      <div>        <Switch>          <Route path='/oauth2callback' componentDidMount={console.log()}>            {() => {              if(window.location.href.includes('/oauth2callback'))              {                oauth2callback(window.location.hash)              }            }}          </Route>          <Route path='/testing' component={Get} />          <Route path='/'>            {/* Function that determines if the user is logged in and can allow the authenticated passage or not. If no, it will not render authenticated pages*/}            {() => {              if(auth.loggedIn())              {                console.log("is logged in")                return(                  <div>                    <Route component={App} history={createHistory}/>                    <div className="row" id="wrapper">                      <Switch>                        {/* <Route path='/screens' component={Screens}/> */}                        <Route path='/screens' component={Screens}/>                        <Route path='/playlists' component={Playlists}/>                        <Route path='/content' component={Content}/>                        <Route path='/help' component={HelpNav}/>                        <Route component={NotFound}/>                      </Switch>                    </div>                  </div>                )              }知道新的反应路由器更新有什么不同导致这个错误存在吗?
查看完整描述

2 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

修复比我想象的要简单得多,新的更新集成了 BrowserRouter 而不是 Router 所以我更换了

<Router history={createHistory}>

<BrowserRouter>

现在一切正常


查看完整回答
反对 回复 2022-12-22
?
手掌心

TA贡献1942条经验 获得超3个赞

在这篇 React Router 6 文章https://medium.com/frontend-digest/whats-new-in-react-router-6-732b06cc83e4上阅读“路由和链接相对于它们的父级”

顺便说一下,有时你有<a>标签而不是链接组件很奇怪


查看完整回答
反对 回复 2022-12-22
  • 2 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号