EN
React - how to create dynamic table
3 points
In this article, we would like to show you how to create a dynamic table in React.
The below example consists of several steps:
- Creating a dynamic table based on array,
- Each table always consists of a header and some data records,
- The header is usually fixed and keeps the same amount of columns,
- While creating data records we use
map()
function. It's a good approach to convert array items into React elements.
Note:
Remember that each element should have a unique key - it helps React optimally manage changes in the DOM. Such a key may be, for example, the
id
assigned to each element of the table.
Runnable example:
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
const tableStyle = {
6
border: '1px solid black',
7
borderCollapse: 'collapse'
8
}
9
10
const tdStyle = {
11
border: '1px solid black',
12
};
13
14
const App = () => {
15
const students = [
16
{ id: 1, name: 'Tom', age: 25 },
17
{ id: 2, name: 'Adam', age: 43 },
18
{ id: 3, name: 'Mark', age: 16 },
19
{ id: 4, name: 'John', age: 29 }
20
];
21
return (
22
<div>
23
<table style={tableStyle}>
24
<tbody>
25
<tr>
26
<th style={tdStyle}>Id</th>
27
<th style={tdStyle}>Name</th>
28
<th style={tdStyle}>Age</th>
29
</tr>
30
{students.map(({ id, name, age }) => (
31
<tr key={id}>
32
<td style={tdStyle}>{id}</td>
33
<td style={tdStyle}>{name}</td>
34
<td style={tdStyle}>{age}</td>
35
</tr>
36
))}
37
</tbody>
38
</table>
39
</div>
40
);
41
};
42
43
const root = document.querySelector('#root');
44
ReactDOM.render(<App />, root );
In this section, we want to show you how to create Table
component which displays data changing on click event.
xxxxxxxxxx
1
// Note: Uncomment import lines during working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
const tableStyle = {
6
border: '1px solid black',
7
borderCollapse: 'collapse'
8
}
9
10
const tdStyle = {
11
border: '1px solid black',
12
};
13
14
const Table = ({students}) => (
15
<div>
16
<table style={tableStyle}>
17
<tbody>
18
<tr>
19
<th style={tdStyle}>Id</th>
20
<th style={tdStyle}>Name</th>
21
<th style={tdStyle}>Age</th>
22
</tr>
23
{students.map(({ id, name, age }) => (
24
<tr key={id}>
25
<td style={tdStyle}>{id}</td>
26
<td style={tdStyle}>{name}</td>
27
<td style={tdStyle}>{age}</td>
28
</tr>
29
))}
30
</tbody>
31
</table>
32
</div>
33
);
34
35
const GROUP_1 = [
36
{ id: 1, name: 'Tom', age: 22 },
37
{ id: 2, name: 'Adam', age: 43 },
38
{ id: 3, name: 'Mark', age: 16 },
39
{ id: 4, name: 'John', age: 29 }
40
];
41
42
const GROUP_2 = [
43
{ id: 1, name: 'Kate', age: 23 },
44
{ id: 2, name: 'Ann', age: 18 }
45
];
46
47
const App = () => {
48
const [students, setStudents] = React.useState(GROUP_1);
49
return (
50
<div>
51
<div>
52
<button onClick={() => setStudents(GROUP_1)}>Show GROUP_1</button>
53
<button onClick={() => setStudents(GROUP_2)}>Show GROUP_2</button>
54
</div>
55
<br />
56
<Table students={students} />
57
</div >
58
);
59
};
60
61
const root = document.querySelector('#root');
62
ReactDOM.render(<App />, root );
Note:
If you want to see example table styling for the first solution, go to this article.