React 组件渲染
列的 render / renderEditor / renderHeader / renderExpandRow 可以直接返回 JSX, 挂载与回收由适配层负责(内部走 createRoot)—— 行被虚拟滚动回收时对应的 root 一起卸载, 不会泄漏。
示例
微应用尚未挂载。
源码
点击查看源码
tsx
import { VirtTableReact, type ReactTableColumn } from '@virt-table/react';
import { faker } from '@faker-js/faker';
interface Row {
id: number;
name: string;
age: number;
city: string;
score: number;
[k: string]: any;
}
function RatingBar({ value }: { value: number }) {
const color = value >= 80 ? '#00b42a' : value >= 60 ? '#ff7d00' : '#f53f3f';
return (
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<div style={{ width: 60, height: 6, background: '#f2f3f5', borderRadius: 3, overflow: 'hidden' }}>
<div style={{ width: `${value}%`, height: '100%', background: color, borderRadius: 3 }} />
</div>
<span style={{ fontSize: 12, color, minWidth: 28 }}>{value}</span>
</div>
);
}
function CityTag({ text }: { text: string }) {
const colors: Record<string, string> = {
'北京': '#165dff', '上海': '#0fc6c2', '杭州': '#7816ff',
'深圳': '#ff7d00', '广州': '#00b42a',
};
const bg = colors[text] || '#86909c';
return (
<span style={{
display: 'inline-block', padding: '2px 8px', borderRadius: 10,
fontSize: 12, color: '#fff', background: bg,
}}>
{text}
</span>
);
}
const list: Row[] = Array.from({ length: 500 }, (_, i) => ({
id: i,
name: faker.person.fullName(),
age: 20 + Math.floor(Math.random() * 40),
city: ['北京', '上海', '杭州', '深圳', '广州'][i % 5]!,
score: Math.floor(Math.random() * 100),
}));
const columns: ReactTableColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 80 },
{
key: 'name',
title: '姓名',
width: 180,
render: ({ value }) => <span style={{ fontWeight: 500, color: '#1d2129' }}>{value}</span>,
},
{ key: 'age', title: '年龄', width: 80 },
{
key: 'city',
title: '城市',
width: 120,
render: ({ value }) => <CityTag text={value} />,
},
{
key: 'score',
title: '分数',
width: 160,
render: ({ value }) => <RatingBar value={value} />,
},
];
export default function RenderDemo() {
return (
<div style={{ padding: 16 }}>
<h3 style={{ margin: '0 0 12px', color: '#1d2129' }}>React 组件渲染单元格</h3>
<div style={{ color: '#86909c', fontSize: 13, marginBottom: 8 }}>
render 返回 JSX,框架自动挂载/卸载
</div>
<div style={{ width: 800, height: 500, border: '1px solid #e5e6eb' }}>
<VirtTableReact
columns={columns}
options={{ list, itemKey: 'id', estimatedSize: 40, buffer: 4, border: true }}
/>
</div>
</div>
);
}说明
vanilla 没有这一层(render 返回的就是 DOM 字符串或元素),所以这一页只在框架端有。
tsx
import { VirtTableReact, type ReactTableColumn } from '@virt-table/react';
import MyTag from './MyTag';
const columns: ReactTableColumn[] = [
// 返回 JSX:适配层负责 mount / unmount
{ key: 'status', title: '状态', width: 120, render: ({ value }) => <MyTag value={value} /> },
// 也可以返回字符串 / DOM 元素,与 vanilla 同义
{ key: 'name', title: '姓名', width: 160, render: ({ value }) => `<b>${value}</b>` },
];三条需要留意:
- 查看态别挂重组件。 滚动时行 DOM 被反复复用,每个返回 JSX 的单元格都要建一个 root, 持续挂载 / 销毁会拖帧。轻量内容直接返回字符串,真组件留给编辑态(只有当前激活的那一格 才会构造),理由与第三方组件库集成那页一样。
columns要 memo 化:每次渲染都造新的列数组会让适配层重建列。用useMemo。- 编辑态里的 JSX 要刷新时调
forceUpdate(),见 React API · 编辑态刷新。
相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
col.render | 返回 JSX / 字符串 / DOM 元素 | React API · 渲染函数返回 JSX |
col.renderEditor / renderHeader / renderExpandRow | 同样支持 JSX | React API · 列级 |
setCellRender(rowKey, colKey, cfg) | 单元格级也能返回 JSX(走 ref) | React API · 单元格级渲染 |
forceUpdate() | 刷新编辑态里的 JSX | 实例方法 |
内容层的优先级(表级 / 列级 / 单元格级)与四层模型见 渲染粒度; 字段全量表在 Vanilla API · 单元格渲染。