简体   繁体   English

反应路由器dom渲染空白页面

[英]React router dom render blank page

I am using react-router-dom to navigate to different pages in my app with some protected routes related to authenticated users.我正在使用react-router-dom导航到我的应用程序中的不同页面,其中包含一些与经过身份验证的用户相关的受保护路由。

The issue is that sometimes the router renders a blank page that forces me to refresh the page to render again.问题是有时路由器会呈现一个空白页面,迫使我刷新页面以再次呈现。

My App.js router:我的 App.js 路由器:

 <Switch>
            <Route path="/" component={HomePage} exact></Route>
            <Route path="/user" component={Authentication} ></Route>
            <Route path="/latest-blogs" component={Articles} exact></Route>
            <Route path="/faq" component={Support} exact></Route>
            <Route path="/contact-us" component={ContactUsPage} exact></Route>
            <Route path="/faq/:id" component={Support} exact></Route>
            <Route path="/blogs" component={Blogs} exact></Route>
            <Route path="/blogs/:id" component={SingleArticle} exact></Route>
            <Route path="/blogs/category/:id" component={Blogs} exact></Route>
            <Route path="/blogs/category/:id/:slug" component={Blogs} exact></Route>
            <Route path="/blogs/:id/:slug" component={SingleArticle} exact></Route>
            <Route path="/fees" component={Fees} exact></Route>
            <GuardedRoute path='/profile' component={Profile} />
            <GuardedRoute path='/deposit' component={Deposit} exact/>
            <GuardedRoute path='/deposit/:id' component={Deposit} exact/>
            <GuardedRoute path='/balance' component={Wallet} />
            <GuardedRoute path='/settings' component={Settings}  />
            <Route component={NotFoundPage} />
            <Redirect to="/" />
  </Switch>
....
<Account>
        <Pages>
          <MetaTags></MetaTags>
          <ConnectivityListener></ConnectivityListener>
          <Router>
            <ScrollToTop />
            {routes}
          </Router>
        </Pages>
  </Account>

My Guard Route:我的守护路线:

const GuardedRoute = ({ component: Component, ...rest }) => {
    const { isAuthenticated,isAuthenticatedAndVerified, userAttrs,isLoading,isLoading2 } = React.useContext(AccountContext);
    return (
        <Route
            {...rest}
            component={props => (
                !isLoading
                    ?
                    (
                        isAuthenticated 
                            ?(
                                !isLoading2?(
                                    isAuthenticatedAndVerified ? 
                                    <Component {...props} />
                                    : (userAttrs.phone_number!==undefined ?  <Redirect to={`/user/verify-code`} exact/> : <Redirect to={`/user/verification-form`} exact/>)
                                ) : ''
                               
                            )
                            :
                            <Redirect to={'/user/login'} exact/>
                    )
                    :
                   ''
            )}
        />
    )
}

export default GuardedRoute;

any suggestions?有什么建议么?

That's because you're not doing anything in your loadings state,那是因为您在loadings state 时没有做任何事情,


component={(props) =>
  !isLoading ? (
    isAuthenticated ? (
      !isLoading2 ? (
        isAuthenticatedAndVerified ? (
          <Component {...props} />
        ) : userAttrs.phone_number !== undefined ? (
          <Redirect to={`/user/verify-code`} exact />
        ) : (
          <Redirect to={`/user/verification-form`} exact />
        )
      ) : (
        "" // <==== HERE 
      )
    ) : (
      <Redirect to={"/user/login"} exact />
    )
  ) : (
    "" // <===== HERE
  )
}

so you need to change those HERE marks with a component or some sort of UI所以你需要用一个组件或某种 UI 来改变那些HERE标记

and you need to check why your loadings states don't change你需要检查为什么你的loadings状态没有改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM