antd 官方的 demo 怎么那么少 jsx - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Jtyczc
V2EX    React

antd 官方的 demo 怎么那么少 jsx

  •  
  •   Jtyczc 2023-08-19 09:46:40 +08:00 3275 次点击
    这是一个创建于 816 天前的主题,其的信息可能已经有所发展或是发生改变。

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

    另外,我想吐槽下,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  
       2023-08-19 13:43:04 +08:00   1
    JSX 是 "syntax extension to Javascript", 样例当然要用最小化实现也就是 Javascript 的语法来写.
    clue
        2
    clue  
       2023-08-19 17:51:56 +08:00
    你说的 jsx 是指用 jsx 的语法声明 columns 吗?第一次知道 antd 支持这样的写法

    但我觉得不应该这样做,因为这里的 jsx columns 传给 Table 组件,最终也会被解析为配置。因为 Table 实际的内容是 thead>tr>th, tbody>tr>td 这些,这个特性其实还是有点违反我的直觉的。
    Jtyczc
        3
    Jtyczc  
    OP
       2023-08-19 23:09:49 +08:00 via Android
    @clue 对啊,可能我是新学 react ,真不习惯声明式写法,喜欢直接写 UI ,难道我走歪路了?
        4
    linyongqianglal  
       2023-08-20 07:50:24 +08:00
    不是,react 写法又多了一个?我对 react 的写法还停留在 hook
    Jtyczc
        5
    Jtyczc  
    OP
       2023-08-20 09:44:32 +08:00
    @linyongqianglal #4 有什么区别吗,展示一下 demo
    theprimone
        6
    theprimone  
       2023-08-21 09:24:30 +08:00   1
    之前 antd 还有 Menu.Item 和 Select.Option 之类的组件的,看看老文档就知道了,这些其实都像 @clue 说的那样还要被父组件内部解析之后才能用,不如直接按定义来,性能还好一些。
    duan602728596
        7
    duan602728596  
       2023-08-21 11:09:12 +08:00
    因为不推荐用 jsx 配置的写法,以前很多组件的 jsx 配置都废弃并被推荐为直接 js 配置了
    lichdkimba
        8
    lichdkimba  
       2023-08-21 11:21:53 +08:00
    Column 还可以这样定义?好像没见过
    Redjue
        9
    Redjue  
       2023-08-21 11:33:58 +08:00
    主要还是考虑性能问题,jsx 实例化是很贵的,能减少就尽量减少。改成配置的话,内部可以直接 memo 缓存,性能会更好一些。
    Wxh16144
        10
    Wxh16144  
       2023-08-21 11:36:20 +08:00
    正如 #2 和 #6 说的, 为了性能而作出的优化。将一些 SubComponents 改成 items 或者 options 的形式。
    CHTuring
        11
    CHTuring  
       2023-08-21 13:30:40 +08:00
    上面说性能的其实可以忽略不计,习惯性问题。
    还有,有没有可能国外的组件库基本是用 SubComponents 的形式...
    opentrade
        12
    opentrade  
       2023-08-21 17:27:51 +08:00
    antd 版本升级太麻烦
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1026 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 85ms UTC 22:54 PVG 06:54 LAX 14:54 JFK 17:54
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86