react 简单table实现
基础表格实现
使用 React 实现简单表格可以通过原生 HTML 的 <table> 标签结合动态数据渲染完成。以下是一个基础示例,展示如何通过数组数据动态生成表格内容:
import React from 'react';
const SimpleTable = ({ data }) => {
return (
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
{data.map((item) => (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.name}</td>
<td>{item.age}</td>
</tr>
))}
</tbody>
</table>
);
};
// 使用示例
const App = () => {
const tableData = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 28 },
];
return <SimpleTable data={tableData} />;
};
动态表头扩展
若表头需要动态生成,可以通过遍历对象的键来渲染:

const DynamicHeaderTable = ({ data }) => {
if (data.length === 0) return null;
const headers = Object.keys(data[0]);
return (
<table border="1">
<thead>
<tr>
{headers.map((header) => (
<th key={header}>{header.toUpperCase()}</th>
))}
</tr>
</thead>
<tbody>
{data.map((row, index) => (
<tr key={index}>
{headers.map((header) => (
<td key={`${index}-${header}`}>{row[header]}</td>
))}
</tr>
))}
</tbody>
</table>
);
};
添加样式与交互
通过 CSS 或库(如 styled-components)美化表格,并增加交互功能(如排序):

import styled from 'styled-components';
const StyledTable = styled.table`
width: 100%;
border-collapse: collapse;
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {
background-color: #f5f5f5;
}
`;
const SortableTable = ({ data }) => {
const [sortConfig, setSortConfig] = React.useState({ key: null, direction: 'asc' });
const sortedData = React.useMemo(() => {
if (!sortConfig.key) return data;
return [...data].sort((a, b) => {
if (a[sortConfig.key] < b[sortConfig.key]) {
return sortConfig.direction === 'asc' ? -1 : 1;
}
if (a[sortConfig.key] > b[sortConfig.key]) {
return sortConfig.direction === 'asc' ? 1 : -1;
}
return 0;
});
}, [data, sortConfig]);
const requestSort = (key) => {
let direction = 'asc';
if (sortConfig.key === key && sortConfig.direction === 'asc') {
direction = 'desc';
}
setSortConfig({ key, direction });
};
return (
<StyledTable>
<thead>
<tr>
{Object.keys(data[0]).map((key) => (
<th key={key} onClick={() => requestSort(key)}>
{key.toUpperCase()} {sortConfig.key === key ? (sortConfig.direction === 'asc' ? '↑' : '↓') : ''}
</th>
))}
</tr>
</thead>
<tbody>
{sortedData.map((row, index) => (
<tr key={index}>
{Object.values(row).map((value, i) => (
<td key={i}>{value}</td>
))}
</tr>
))}
</tbody>
</StyledTable>
);
};
使用第三方库
对于复杂需求(如分页、过滤),可考虑使用现成库:
安装示例(React Table):
npm install react-table
import { useTable } from 'react-table';
const ReactTableExample = ({ columns, data }) => {
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({
columns,
data,
});
return (
<table {...getTableProps()}>
<thead>
{headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => (
<td {...cell.getCellProps()}>{cell.render('Cell')}</td>
))}
</tr>
);
})}
</tbody>
</table>
);
};






