Answer the question
In order to leave comments, you need to log in
How to add row to table in ReactJS?
Good day!
I decided to learn a little ReacJS. While I'm trying to create something like "dynamic tables", with simple functionality - add, delete a row.
The project was created via react-create-app (so as not to manually configure packers, Babel, etc.).
I more or less mastered the basics and managed to draw a table:
https://jsfiddle.net/69z2wepo/63374/ I
started looking for information on "dynamic inserts" (for English-language resources), but did not find anything useful. Somewhere something about refs, somewhere something about states, examples and methods are very scattered in implementations, and in general it is extremely difficult to understand something from them from the answers (if any).
The fact that I write in ES6 (through React.Component inheritance) also adds problems, and almost all examples that showed the functionality I needed were written through React.createClass, for example:
https://github.com/eajitesh/Hello- ReactJS/blob/mas...
In general, the question is: How to correctly add a row to the table (as well as any element to some place in the tree)?
Answer the question
In order to leave comments, you need to log in
If you want to change something in the current component, then use the state ( setState ).
If you want to change the child component, change the state of the current one ( setState ) and pass the changed data from the state ( state ) to the child component via props .
In your example, the root component is a Table , to which you pass props : head_names and rows . If you want to make it possible to add a new row to the Table , then you should make a container in which to place the Tableand a button (or other element) for adding new rows. In the state ( state ) of the container, you need to add head_names and rows , on the basis of which the table will be created.
class TableManagement extends React.Component {
constructor(props) {
super(props);
this.state = {
head_names: ['qwe0', 'qwe1'],
rows: [
[1, 2],
[3, 4]
]
};
}
AddRow() {
let newRows = this.state.rows;
newRows.push([0, 0]);
this.setState({rows: newRows});
}
render() {
return (
<div>
<Table head={this.state.head_names} rows={this.state.rows} />
<hr />
<button onClick={ this.AddRow.bind(this) }>Add row</button>
</div>
);
}
}
class Table extends React.Component {
render() {
return (
<table>
<thead>
{this.genHead()}
</thead>
<tbody>
{this.genRow()}
</tbody>
</table>
);
}
genHead() {
var head = this.props.head;
return head.map(function(v, i) {
return (
<th key={'th' + i}>
{v}
</th>
);
});
}
genRow() {
var rows = this.props.rows;
return rows.map(function(v, i) {
var tmp = v.map(function(v2, j) {
return (
<td key={'td' + i + '_' + j}>
{v2}
</td>
);
});
return (
<tr key={'tr' + i}>
{tmp}
</tr>
)
});
}
}
ReactDOM.render(
<TableManagement />,
document.getElementById('root')
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question