在不同组件之间共享usestate reactjs
因此,我一直在尝试建立一个预订系统,每个用户只能预订问题后如何检查如何登录哪个用户。
组件登录名称具有以下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,有两种方法可以在React应用程序中存储UserDetails,
方法1:
您可以将用户详细信息存储在
context> contextapi
中,并使用它。使用上下文应用。您可以使用createContext
创建上下文,usecontext
将其累积。使用以下React JS文档链接进行参考,https://reactjs.s.org/docs/context.htext.html
方法2:
这是最简单的方法。您还可以使用
localStorage
存储用户详细信息,并从localstorage
的任何页面上检索。将用户删除存储在
localstorage
中。使用以下代码,从
localstorage
中检索用户删除。您可以使用以下代码,注意:
您不应在这两种方法中存储密码。
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 usecreateContext
to create a context anduseContext
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 fromlocalStorage
.To store the userdetails in the
localStorage
. Use the below code,To retrieve the userdetails from the
localStorage
. You can use the below code,Note :
You should not store passwords in both the approaches.