简体   繁体   English

当访问正确的 url 并且用户未登录时,如何安装我的 React 组件?

[英]How can I make my React Component mount, when the right url is accessed and the user is not logged in?

My App.js:我的 App.js:

        <Nav info={this.props}/>
        {this.props.loading === true
          ? null
          : <div>
            {this.props.authedUser
              ? <Switch>
                  <Route path='/' exact component={Dashboard}/>
                  <Route path='/question/:id' exact component={QPage}/>
                  <Route path='/new' exact component={CreateQ}/>
                  <Route path='/leaderboard' exact component={Leaderboard}/>
                  <Route path='/sign-in' exact component={SignIn}/>
                  <Route component={Error}/> 
                </Switch>
              : <Route path='/sign-in' component={SignIn}/>
            }                  
            </div>
        }

My app shows a Nav bar at the top.我的应用程序在顶部显示了一个导航栏。 It has a button that says Logout .它有一个按钮,上面写着Logout When the button is clicked, I am clearing the authedUser behind the scenes in my Redux store and navigate to the '/sign-in' url.单击该按钮后,我将在 Redux 商店中清除幕后的authedUser并导航到“/登录”url。 All of this works, but when the '/sign-in' url is accessed, my SignIn component doesn't get rendered.所有这些都有效,但是当访问 '/sign-in' url 时,我的SignIn组件不会被渲染。 But when I reload, the SignIn component does get rendered.但是当我重新加载时, SignIn组件会被渲染。

After the user is being logged out, App.js get re-rendered.用户退出后,App.js 会重新渲染。 Then the ternary operator should actually be false (because this.props.authedUser is null) and it should render the route and its component SignIn .那么三元运算符实际上应该为 false(因为 this.props.authedUser 为空)并且它应该呈现路由及其组件SignIn But this doesn't seem to happen.但这似乎不会发生。 How can I make sure, that the SignIn component does get rendered, when the ternary operator returns false?当三元运算符返回 false 时,如何确保SignIn组件确实被渲染?

When authedUser is false app.js is re-render with outauthedUser为 false 时 app.js 会重新渲染

<Switch>
  <Route path='/' exact component={Dashboard}/>
  <Route path='/question/:id' exact component={QPage}/>
  <Route path='/new' exact component={CreateQ}/>
  <Route path='/leaderboard' exact component={Leaderboard}/>
  <Route path='/sign-in' exact component={SignIn}/>
  <Route component={Error}/> 
</Switch>

so... when you navigate the user to /sign-in the route those not exists所以...当您将用户导航到/sign-in那些不存在的路线时

In order to achive your goal, you can do this:为了实现您的目标,您可以这样做:

App.js

<Switch>
  <Route path='/' exact component={Dashboard}/>
  <Route path='/main' exact component={Main}/>
  <Route path='/sign-in' exact component={SignIn}/>
  <Route component={Error}/> 
</Switch>

After a succesfull login, you will do history.push('/main'), and in Main.js all the authenticated routes will be managed成功登录后,您将执行 history.push('/main'),在 Main.js 中将管理所有经过身份验证的路由

Main.js
... your code...
if (!this.props.authedUser) {
  return <Redirect to='/sign-in' />
};

return (
  <Switch>
    <Route path='/question/:id' exact component={QPage}/>
    <Route path='/new' exact component={CreateQ}/>
    <Route path='/leaderboard' exact component={Leaderboard}/>
  </Switch>
)

and when you will want to navigate a user to some route:当您想要将用户导航到某个路线时:

history.push('/main/leaderboard')

暂无
暂无

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

相关问题 复制每个组件安装的对象。 我如何使其只能运行一次? 在反应 - Objects duplicating every component mount. How can I make it run only once ? In react 如何让 header 组件知道用户是否登录 React? - How to make header component know if user is logged in in React? 当用户单击按钮时,如何保存可重复使用的React组件? - How do i make my reusable react component saves when the user clicks button? 如果用户未登录,如何使用React-router重定向到登录页面来制作React应用? - How can I make a React app using React-router redirect to a login page if the user is not logged in? 使用laravel身份验证时,如何检查用户是否已在“反应”组件中“登录”? - How do I check if user is 'logged in' in a react component when using laravel authentication? 如何在 React JS 的 ModalHeader 组件中右对齐我的按钮组件 - How can I right-aligned my Button Component in ModalHeader Component in React JS React:如何有效地制作我的组件,以便在道具更改时整个组件不会重新渲染? - React: How can I efficiently make my component so the entire component doesn't re-render when a prop changes? 如何在主页上隐藏导航栏并使其仅在用户登录后才可见? - How can I hide my navbar on the homepage and make it only visible once the user has logged in? 当我未登录时,如何使我的组合菜单消失? - How i can make disappeared my composant Menu when i'm not logged in? 当浏览器刷新时,如何在 React/Redux web 应用程序中保留由 JWT 身份验证的登录用户? - How can I persist a logged-in user authenticated by JWT in React/Redux web application when the browser refreshes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM