使用不同的页面模板用于React登录/注册页面?

huangapple go评论76阅读模式
英文:

Using different page template for React Login/Register page?

问题

I use an Admin Dashboard in my React app and now I will add a Login and Register pages to the app. However, I am not sure how can I make login and register routes to use a different template than the app. I trşed something below, but it is too ugly and I think there would be a better way in React.

Note: I thought to check if the user logged in or not in App.js or index.js and then use the necessary component e.g. navbar.

export default function App() {
  return (
    <div className="App">
      <BrowserRouter>
        {localStorage.getItem("currentUser") != null ? <AppBar></AppBar> : null}
        <Switch>
          <Route exact path="/create" component={Form} />

          // ...
        </Switch>
      </BrowserRouter>
    </div>
  );
}

The second issue is, should I use an AuthContextProvider to check if the user authenticated or not instead of localStorage.getItem("currentUser") != null as mentioned in the following approach? Any idea?

https://stackoverflow.com/questions/61892504/redirect-to-page-with-different-page-layout-in-react

英文:

I use an Admin Dashboard in my React app and now I will add a Login and Register pages to the app. However, I am not sure how can I make login and register routes to use a different template than the app. I trşed something below, but it is too ugly and I think there would be a better way in React.

Note: I thought to check if the user logged in or not in App.js or index.js and then use the necessary component e.g. navbar.

export default function App() {
  return (
    <div className="App">
      <BrowserRouter>
        {localStorage.getItem("currentUser") != null ? <AppBar></AppBar> : null}
        <Switch>
          <Route exact path="/create" component={Form} />

          // ...
        </Switch>
      </BrowserRouter>
    </div>
  );
}

The second issue is, should I use an AuthContextProvider to check if the user authenticated or not instead of localStorage.getItem("currentUser") != null as metioned on the following approach? Any idea?

https://stackoverflow.com/questions/61892504/redirect-to-page-with-different-page-layout-in-react

答案1

得分: 0

以下是代码的翻译部分:

Index.js

root.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

App.js

export default function App() {
  return (
    <div className="App">
      <Switch>
        {localStorage.getItem("currentUser") != null ? <AppBar /> : null}
        <Switch>
          <Route exact path="/create" component={<Form />} />
          <Route exact path="/register" component={<Register />} /> // 注册组件
          <Route exact path="/login" component={<Login />} /> // 登录组件
        </Switch>
      </Switch>
    </div>
  );
}
英文:

Following solution might help you :

Index.js

root.render(
  &lt;React.StrictMode&gt;
    &lt;BrowserRouter&gt;
    &lt;App /&gt;
    &lt;/BrowserRouter&gt;
  &lt;/React.StrictMode&gt;
);

App.js

export default function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
&lt;Switch&gt;
        {localStorage.getItem(&quot;currentUser&quot;) != null ? &lt;AppBar /&gt; : null}
        &lt;Switch&gt;
          &lt;Route exact path=&quot;/create&quot; component={&lt;Form /&gt;} /&gt;
         &lt;Route exact path=&quot;/register&quot; component={&lt;Register /&gt;} /&gt; //register component
         &lt;Route exact path=&quot;/login&quot; component={&lt;Login /&gt;} /&gt; //login component
&lt;/Switch&gt;
    &lt;/div&gt;

);
}

huangapple
  • 本文由 发表于 2023年3月7日 19:15:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定