在不同组件之间共享usestate reactjs

发布于 2025-02-14 01:11:04 字数 220 浏览 0 评论 0原文

因此,我一直在尝试建立一个预订系统,每个用户只能预订问题后如何检查如何登录哪个用户。

组件登录名称具有以下USESESTATE:

const [details, setDetails] = useState({ room: "", password: "" });

无论如何是否有将USESTATE导入另一个组件?如果没有,可以检查哪个用户已登录?

So I've been trying to make a booking system, each user can only book once the problem being how to check which user is logged in.

The component LoginForm has the following useState:

const [details, setDetails] = useState({ room: "", password: "" });

Is there anyway to import the useState to another component? If not is there anyway to to check which user is logged in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2025-02-21 01:11:04

据我所知,有两种方法可以在React应用程序中存储UserDetails,

方法1:

您可以将用户详细信息存储在context> contextapi中,并使用它。使用上下文应用。您可以使用createContext创建上下文,usecontext将其累积。使用以下React JS文档链接进行参考,

https://reactjs.s.org/docs/context.htext.html

方法2:

这是最简单的方法。您还可以使用localStorage存储用户详细信息,并从localstorage的任何页面上检索。

将用户删除存储在localstorage中。使用以下代码,

localStorage.setItem("userDetails", {username, email, mobileno});

localstorage中检索用户删除。您可以使用以下代码,

let userdetails = localStorage.getItem("userDetails");

注意:

您不应在这两种方法中存储密码。

As far as I have seen, There are 2 approaches to store userdetails in the react application,

Approach 1:

You can store the user details in the contextApi and use it accross the application by using the context. You can use createContext to create a context and useContext to use it accross the application. Use the below react js documentation link for the reference,

https://reactjs.org/docs/context.html

Approach 2:

This is the easiest approach. You can also use the localStorage to store the user details and retrieve on any page you want from localStorage.

To store the userdetails in the localStorage. Use the below code,

localStorage.setItem("userDetails", {username, email, mobileno});

To retrieve the userdetails from the localStorage. You can use the below code,

let userdetails = localStorage.getItem("userDetails");

Note :

You should not store passwords in both the approaches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文