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

在更新了我们的一堆依赖项之后,我们现在被迫使用导致重定向的锚标记,在我们使用之前但现在每次我们点击它都会转到正确的 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>

                )

              }


知道新的反应路由器更新有什么不同导致这个错误存在吗?


慕娘9325324
浏览 99回答 2
2回答

慕神8447489

修复比我想象的要简单得多,新的更新集成了 BrowserRouter 而不是 Router 所以我更换了<Router&nbsp;history={createHistory}>和<BrowserRouter>现在一切正常

手掌心

在这篇 React Router 6 文章https://medium.com/frontend-digest/whats-new-in-react-router-6-732b06cc83e4上阅读“路由和链接相对于它们的父级”顺便说一下,有时你有<a>标签而不是链接组件很奇怪
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript