为什么JavaScript渲染删除的组件
我现在正在研究一个项目,我有一个由ANTD 菜单
项目返回的组件。它有4个组件:home
,用户名
,登录
和register> register
:
const items = [
{
label: <Link to='/'>Home </Link>, //- {JSON.stringify(user)}
key: 'home',
icon: <AppstoreOutlined />,
},
{
label: 'Username',
key: 'SubMenu',
icon: <SettingOutlined />,
children: [
{
label: 'Option 1',
key: 'setting:1',
},
{
label: 'Option 2',
key: 'setting:2',
},
{
label: <item onClick={logout}>Logout</item>,
key: 'setting:3',
icon: <LogoutOutlined />
},
],
},
{
label: <Link to='/register'>Register</Link>,
key: 'register',
icon: <UserAddOutlined />,
style: { marginLeft: 1030}, // problem: hard-coded margin
},
{
label: <Link to='/login'>Login</Link>,
key: 'login',
icon: <UserOutlined />,
style: { marginLeft: 'auto'},
},
];
useEffect(() => {
if (user) { // "user" is an object returned by userSelector
items.splice(2, 2);
}
}, [user]);
console.log(items);
return <Menu onClick={handleClick} selectedKeys={[current]} mode="horizontal" items={items} />;
您可以看到,我是在此处使用使用> useeffect
挂钩。如果未登录用户(so user === null
),则不会执行useFeffect
中的if语句;否则,登录
寄存器数组中的组件项目
将被删除并隐藏,只剩下前2个。
从console.log(项目);
我们可以看到项目
当前的内容。当用户登录时,它确实只有两个组件:
但是,在这种情况下,我们仍然可以看到登录
和register
在那里:
那么为什么JS和反应渲染删除的组件?
Guys I'm working on a project now and I have a component that is returned by antd Menu
item. It has 4 components: Home
, Username
, Login
, and Register
:
const items = [
{
label: <Link to='/'>Home </Link>, //- {JSON.stringify(user)}
key: 'home',
icon: <AppstoreOutlined />,
},
{
label: 'Username',
key: 'SubMenu',
icon: <SettingOutlined />,
children: [
{
label: 'Option 1',
key: 'setting:1',
},
{
label: 'Option 2',
key: 'setting:2',
},
{
label: <item onClick={logout}>Logout</item>,
key: 'setting:3',
icon: <LogoutOutlined />
},
],
},
{
label: <Link to='/register'>Register</Link>,
key: 'register',
icon: <UserAddOutlined />,
style: { marginLeft: 1030}, // problem: hard-coded margin
},
{
label: <Link to='/login'>Login</Link>,
key: 'login',
icon: <UserOutlined />,
style: { marginLeft: 'auto'},
},
];
useEffect(() => {
if (user) { // "user" is an object returned by userSelector
items.splice(2, 2);
}
}, [user]);
console.log(items);
return <Menu onClick={handleClick} selectedKeys={[current]} mode="horizontal" items={items} />;
As you can see, I'm using a useEffect
hook here. If the user is not logged in (so user === null
), then the if statements in useEffect
will not be executed; otherwise the Login
and Register
components in the array items
will be removed and hidden, and only the first 2 are left.
From console.log(items);
we can see what items
is like currently. When a user is logged in, it does have only two components:
However, we can still see Login
and Register
are there in such a circumstance:
So why do JS and React render removed components?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要重新渲染。您可以将数组存储在状态下。如果将数组存储在普通JavaScript变量中。 React不会重新渲染组件。像我在下面给出的那样尝试
If you need the re render to be happened. You can store the array in a state. If you store the array in normal javascript variable. React will not re render the component. Try like I given below,
您需要使用“ usestate”挂钩进行UI更新,例如,这些项目将被更新,但是UI不会更新。
You need to use "useState" hook to make the UI update, in you example, the items will be updated, but the UI will not update.