【Ant Design】【List】

源码

javascript 复制代码
   <List
       style={{
          width: 600,
          height: 200,
          overflow: 'auto',
        }}
        header={
          <>
            <Typography.Text
              copyable={{
                text: file.response.join(',') || '',
              }}
            />
          </>
        }
        split={false}
        dataSource={file.response}
        renderItem={(item: any, index: number) => (
          <List.Item>
            <Typography.Text type="danger">{item}</Typography.Text>
          </List.Item>
        )}
      />

报错

Uncaught TypeError: Cannot read properties of null (reading 'key')

分析

第一反应是每个字项遍历出了问题

加上key,改为

复制代码
 <List.Item key={`${item}-${index}`}>
        <Typography.Text type="danger">{item}</Typography.Text>
   </List.Item>

依然报错,那就是原始数据有null,不支持了

将数据注入List组件时,再用String包一层,防止存在null

复制代码
 dataSource={file.response.map(String)}

这样就没问题了

相关推荐
东华帝君21 小时前
React源码解读
前端
Mintopia1 天前
🌱 AIGC 技术的轻量化趋势:Web 端“小而美”模型的崛起
前端·javascript·aigc
开发者小天1 天前
React中的useRef的用法
开发语言·前端·javascript·react.js
im_AMBER1 天前
React 11 登录页项目框架搭建
前端·学习·react.js·前端框架
Live&&learn1 天前
nvm切换node版本时,npm不跟着切换解决
前端·npm·node.js
xixixin_1 天前
【React】检测元素是否出现在用户视窗内
开发语言·前端·javascript·react.js
谢彦超oooo1 天前
HTML5 与前端开发要点
前端·html·html5
IT_陈寒1 天前
Vue 3响应式原理深度拆解:5个90%开发者不知道的Ref与Reactive底层实现差异
前端·人工智能·后端
睡前要喝豆奶粉1 天前
在.NET Core Web Api中使用JWT并配置UserContext获取用户信息
前端·.netcore
前端加油站1 天前
一份实用的Vue3技术栈代码评审指南
前端·vue.js