javascript - 为什么 javascript 会渲染移除的组件

标签 javascript reactjs antd use-effect

伙计们,我现在正在开发一个项目,我有一个由 antd Menu 项返回的组件。它有 4 个组件:HomeUsernameLoginRegister:

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 Hook 。如果用户没有登录(所以user === null),那么useEffect中的if语句将不会被执行;否则数组 items 中的 LoginRegister 组件将被删除并隐藏,只剩下前 2 个。

console.log(items);我们可以看到items当前是什么样的。当用户登录时,它只有两个组件: enter image description here

但是,在这种情况下我们仍然可以看到LoginRegister存在: enter image description here

那么为什么 JS 和 React 会渲染移除的组件呢?

最佳答案

如果您需要重新渲染。您可以将数组存储在某种状态中。如果将数组存储在普通的 javascript 变量中。 React 不会重新渲染组件。尝试像我下面给出的那样,

const [items, setItems] = useState([
        {
          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
        setItems(items.splice(2, 2));
      }
    }, [user]);
    console.log(items);
  
    return <Menu  onClick={handleClick} selectedKeys={[current]} mode="horizontal" items={items} />;

关于javascript - 为什么 javascript 会渲染移除的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72675510/

相关文章:

javascript - 如何替换 REACT.js 中的 refs(antd 表示例)

reactjs - 使用extends React.Component机制正确创建antd Form

javascript - 从 javascript Node.js 调用 mongoexport

javascript - 如何用阿芙罗狄蒂覆盖 material-ui 类名?

reactjs - react Material 表

reactjs - 在 ANTD React 中使用全选选项进行多选

javascript - 正则表达式 - 匹配类似 IRC 的参数?

JavaScript 正则表达式匹配忽略文字点

Javascript 现在提前或落后几个小时

javascript - 避免 Nodejs/react-native 的 Realm.open 中的回调 hell