antd 官方的 demo 怎么那么少 jsx

265 天前
 Jtyczc

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

另外,我想吐槽下,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>
2317 次点击
所在节点    React
12 条回复
israinbow
265 天前
JSX 是 "syntax extension to JavaScript", 样例当然要用最小化实现也就是 JavaScript 的语法来写.
clue
265 天前
你说的 jsx 是指用 jsx 的语法声明 columns 吗?第一次知道 antd 支持这样的写法

但我觉得不应该这样做,因为这里的 jsx columns 传给 Table 组件,最终也会被解析为配置。因为 Table 实际的内容是 thead>tr>th, tbody>tr>td 这些,这个特性其实还是有点违反我的直觉的。
Jtyczc
265 天前
@clue 对啊,可能我是新学 react ,真不习惯声明式写法,喜欢直接写 UI ,难道我走歪路了?
linyongqianglal
264 天前
不是,react 写法又多了一个?我对 react 的写法还停留在 hook
Jtyczc
264 天前
@linyongqianglal #4 有什么区别吗,展示一下 demo
theprimone
263 天前
之前 antd 还有 Menu.Item 和 Select.Option 之类的组件的,看看老文档就知道了,这些其实都像 @clue 说的那样还要被父组件内部解析之后才能用,不如直接按定义来,性能还好一些。
duan602728596
263 天前
因为不推荐用 jsx 配置的写法,以前很多组件的 jsx 配置都废弃并被推荐为直接 js 配置了
lichdkimba
263 天前
Column 还可以这样定义?好像没见过
Redjue
263 天前
主要还是考虑性能问题,jsx 实例化是很贵的,能减少就尽量减少。改成配置的话,内部可以直接 memo 缓存,性能会更好一些。
Wxh16144
263 天前
正如 #2 和 #6 说的, 为了性能而作出的优化。将一些 SubComponents 改成 items 或者 options 的形式。
CHTuring
263 天前
上面说性能的其实可以忽略不计,习惯性问题。
还有,有没有可能国外的组件库基本是用 SubComponents 的形式...
opentrade
263 天前
antd 版本升级太麻烦

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/966594

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX