简体   繁体   English

登录后如何呈现可能的主页?

[英]How do I render may home page after login?

I'm new in react, developing a web page as a project, I want when a user open my web page, he will have only Login and Registration component will be there, and NO header or navigation bar will be there, after user successfully login then he will have a header, navbar and other components, and all the Link, Route and Routes will be there, I am unable to understand what to render in App.js, that is my App.js code, in Login component there is Login & registration form.我是反应新手,将网页开发为项目,我希望当用户打开我的网页时,他将只有登录和注册组件,并且在用户成功后不会出现标题或导航栏login 然后他会有一个 header、navbar 和其他组件,所有的 Link、Route 和 Routes 都会在那里,我无法理解在 App.js 中呈现什么,即我的 App.js 代码,在 Login 组件那里是登录和注册表。 From Login I render Dashboard(Home page).从登录我呈现仪表板(主页)。

How to do that any idea ?怎么做?

 import './App.css'; import {Login} from "./loginPage"; function App() { return ( <> <Login/> </> ); } export default App;

You can conditionally render elements.您可以有条件地渲染元素。 For example, let's say you have some state value:例如,假设您有一些状态值:

const [showLogin, setShowLogin] = useState(true);

Then you can use that value to conditionally render one thing or the other:然后您可以使用该值有条件地渲染一件事或另一件事:

return (
  <>
    {showLogin ? <Login/> : <Dashboard/>}
  </>
);

So in this case when showLogin is true then the <Login/> component renders, otherwise the <Dashboard/> component renders.因此,在这种情况下,当showLogintrue时, <Login/>组件将呈现,否则<Dashboard/>组件将呈现。

The question then becomes...那么问题就变成了……

What state value will you use and how will you define your condition?您将使用什么状态值以及如何定义您的条件?

That part is really up to you.那部分真的取决于你。 Whatever state is being modified by <Login/> to indicate that the user has successfully logged in, that's the state you would examine in your condition to determine if the user should see the login prompt or the dashboard interface (or whatever you're showing). <Login/>正在修改任何状态以指示用户已成功登录,这就是您将在您的条件下检查以确定用户是否应该看到登录提示或仪表板界面(或您正在显示的任何内容)的状态)。

That <Dashboard/> can of course be anything. <Dashboard/>当然可以是任何东西。 For example, if you're using a routing system, you might render that instead:例如,如果您使用的是路由系统,则可以改为渲染它:

return (
  <>
    {showLogin ? <Login/> : 
      <Router>
        <!-- your routes here, etc. -->
      </Router>
    }
  </>
);

Any JSX hierarchy will work, just like it would work anywhere else in the JSX.任何 JSX 层次结构都可以工作,就像它可以在 JSX 中的任何其他地方工作一样。

Here you can create a state called "loggedIn" or anyting or your choice.在这里,您可以创建一个名为“loggedIn”的状态或任何状态或您的选择。 and add condition (you can use ternary operator) for if its true then render the component.并添加条件(您可以使用三元运算符)如果其为真则渲染组件。

const [loggedIn, setLoggedIn] = useState(false);

{loggedIn ? <Navbar />: ""}
{loggedIn ? <Header /> : ""}

This way even when the session expires and user is logged out u just have to change one state and these components will be hidden again.这样,即使会话到期并且用户已注销,您也只需更改一个状态,这些组件将再次隐藏。

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

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