如何根据用户类型显示正确的组件
我的React Project正常用户和管理员中有两种不同的用户类型,但要指定我没有使用任何数据库。因此,我将MetAmask钱包连接起来,并且我得到了钱包(您可以将其视为ID),如果钱包相等,则它是管理员,否则是正常的用户。在LocalStorage中,如果管理员为真或错误,我要保留,并且根据我要显示的。但是首先,我的if语句有问题,因为它一直显示(即使在localstorage admin中也是false),其次不是实时渲染。因此,我期望的是,当用户类型更改时,它将重新启动并显示正确的组件。这是我的组件:
import React, { useEffect } from "react";
import { Container, Row, Col } from "react-bootstrap";
import Twitch from "../Twitch/Twitch";
import AdminGame from "./AdminGame";
import "./Game.css";
import UserGame from "./UserGame";
const Game = () => {
useEffect(() => {
if (
localStorage.getItem("walletId") ==
""
) {
localStorage.setItem("admin", true);
} else {
localStorage.setItem("admin", false);
}
}, [localStorage.getItem("walletId")]);
return (
<section id="game">
<Container>
<Row>
<Col>
{localStorage.getItem('admin') ? <UserGame /> : <AdminGame/>}
</Col>
</Row>
</Container>
</section>
);
};
export default Game;
容器和行标签是基于React Bootstrap的,因此不必担心它们。但是我想知道我的其余逻辑是正确的,为什么它不呈现。
I have two different user type in my react project normal user and admin but to specify that I am not using any database. So I connect metamask wallet and I am getting and the wallet (you can think as an Id) and if the wallet is equal something it is admin, otherwise it is normal user. in localStorage I am keeping if the admin is true or false and according to that I want to show or . But first there is something wrong with my if statement, because all the time it shows (even in localStorage admin is false) and second it is not real time rendering. So what I am expecting, when the user type is changed, it the project rerenders and show the correct component. Here is my component:
import React, { useEffect } from "react";
import { Container, Row, Col } from "react-bootstrap";
import Twitch from "../Twitch/Twitch";
import AdminGame from "./AdminGame";
import "./Game.css";
import UserGame from "./UserGame";
const Game = () => {
useEffect(() => {
if (
localStorage.getItem("walletId") ==
""
) {
localStorage.setItem("admin", true);
} else {
localStorage.setItem("admin", false);
}
}, [localStorage.getItem("walletId")]);
return (
<section id="game">
<Container>
<Row>
<Col>
{localStorage.getItem('admin') ? <UserGame /> : <AdminGame/>}
</Col>
</Row>
</Container>
</section>
);
};
export default Game;
Container and Row tags are React Bootstrap based so dont worry about them. But I wonder rest of my logic is correct and why it doesnt rerender.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该做JSON.PARSE,以便您的评估正确。
you should do JSON.parse so that your evaluation is correct.