V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Jtyczc
V2EX  ›  React

antd 官方的 demo 怎么那么少 jsx

  •  
  •   Jtyczc · 252 天前 · 2295 次点击
    这是一个创建于 252 天前的主题,其中的信息可能已经有所发展或是发生改变。

    好像就喜欢配置写法。。,为什么呢?

    另外,我想吐槽下,Dropdown 组件在 table 中太难用了,试了好多次才拿到每行的数据...

    官方 demo:

    const handleMenuClick = (e) => {
      message.info('Click on menu item.');
      console.log('click', e);
    };
    const items = [
      {
        label: '1st menu item',
        key: '1',
        icon: <UserOutlined />,
      },
      {
        label: '2nd menu item',
        key: '2',
        icon: <UserOutlined />,
      },
      {
        label: '3rd menu item',
        key: '3',
        icon: <UserOutlined />,
        danger: true,
      },
      {
        label: '4rd menu item',
        key: '4',
        icon: <UserOutlined />,
        danger: true,
        disabled: true,
      },
    ];
    const menuProps = {
      items,
      onClick: handleMenuClick,
    };
    
    <Dropdown menu={menuProps}>
          <Button>
            <Space>
              Button
              <DownOutlined />
            </Space>
          </Button>
        </Dropdown>
    

    我的 demo:

    const handleMenuClick = (e, record) => {
        console.log('e', e)
        console.log('record', record)
        if (e.key === 'updateRole') {
          handleShowUpdateRoleDialog(record.adminId)
        }
        if (e.key === 'edit') {
          handleShowEditDialog(record.adminId)
        }
        if (e.key === 'resetPassword') {
          handleShowResetPasswordDialog(record.adminId)
        }
        if (e.key === 'delete') {
          handleShowDeleteDialog(record.adminId)
        }
      }
    
      const items = [
        {
          label: '分配角色',
          key: 'updateRole',
        },
        {
          label: '编辑',
          key: 'edit',
        },
        {
          label: '重置密码',
          key: 'resetPassword',
          danger: true,
        },
        {
          label: '删除',
          key: 'delete',
          danger: true,
        },
      ]
    
      // 点击下拉事件
      const getMenuProps = (record) => {
        return {
          items,
          onClick: (e) => handleMenuClick(e, record),
        }
      }
    
     <Table
              dataSource={userList}
              rowKey={(record) => record.adminId}
              pagination={false}
              loading={loading}
            >
              <Column title="编号" dataIndex="adminId"></Column>
              <Column title="账号" dataIndex="username"></Column>
              <Column title="昵称" dataIndex="nickName"></Column>
              <Column title="邮箱" dataIndex="email"></Column>
              <Column
                title="添加时间"
                dataIndex="createTime"
                render={(_, record) =>
                  record.createTime
                    ? dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss')
                    : '-'
                }
              />
              <Column
                title="最后登录"
                dataIndex="loginTime"
                render={(_, record) =>
                  record.loginTime
                    ? dayjs(record.loginTime).format('YYYY-MM-DD HH:mm:ss')
                    : '-'
                }
              />
              <Column
                title="是否启用"
                dataIndex="activeState"
                render={(_, record) => (
                  <Switch
                    checked={record.activeState == 1}
                    onClick={(checked) => {
                      updateState(record.adminId, checked ? 1 : 0)
                    }}
                  />
                )}
              />
              <Column
                title="操作"
                render={(text, record) => (
                  <Dropdown menu={getMenuProps(record)}>
                    <Button>
                      <Space>
                        操作
                        <DownOutlined />
                      </Space>
                    </Button>
                  </Dropdown>
                )}
              />
            </Table>
    
    12 条回复    2023-08-21 17:27:51 +08:00
    israinbow
        1
    israinbow  
       252 天前   ❤️ 1
    JSX 是 "syntax extension to JavaScript", 样例当然要用最小化实现也就是 JavaScript 的语法来写.
    clue
        2
    clue  
       252 天前
    你说的 jsx 是指用 jsx 的语法声明 columns 吗?第一次知道 antd 支持这样的写法

    但我觉得不应该这样做,因为这里的 jsx columns 传给 Table 组件,最终也会被解析为配置。因为 Table 实际的内容是 thead>tr>th, tbody>tr>td 这些,这个特性其实还是有点违反我的直觉的。
    Jtyczc
        3
    Jtyczc  
    OP
       252 天前 via Android
    @clue 对啊,可能我是新学 react ,真不习惯声明式写法,喜欢直接写 UI ,难道我走歪路了?
    linyongqianglal
        4
    linyongqianglal  
       251 天前
    不是,react 写法又多了一个?我对 react 的写法还停留在 hook
    Jtyczc
        5
    Jtyczc  
    OP
       251 天前
    @linyongqianglal #4 有什么区别吗,展示一下 demo
    theprimone
        6
    theprimone  
       250 天前   ❤️ 1
    之前 antd 还有 Menu.Item 和 Select.Option 之类的组件的,看看老文档就知道了,这些其实都像 @clue 说的那样还要被父组件内部解析之后才能用,不如直接按定义来,性能还好一些。
    duan602728596
        7
    duan602728596  
       250 天前
    因为不推荐用 jsx 配置的写法,以前很多组件的 jsx 配置都废弃并被推荐为直接 js 配置了
    lichdkimba
        8
    lichdkimba  
       250 天前
    Column 还可以这样定义?好像没见过
    Redjue
        9
    Redjue  
       250 天前
    主要还是考虑性能问题,jsx 实例化是很贵的,能减少就尽量减少。改成配置的话,内部可以直接 memo 缓存,性能会更好一些。
    Wxh16144
        10
    Wxh16144  
       250 天前
    正如 #2 和 #6 说的, 为了性能而作出的优化。将一些 SubComponents 改成 items 或者 options 的形式。
    CHTuring
        11
    CHTuring  
       250 天前
    上面说性能的其实可以忽略不计,习惯性问题。
    还有,有没有可能国外的组件库基本是用 SubComponents 的形式...
    opentrade
        12
    opentrade  
       250 天前
    antd 版本升级太麻烦
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1480 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:58 · PVG 00:58 · LAX 09:58 · JFK 12:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.