I am learning react and playing around it. My existing project is using java from the backend and planning to integrate React at the frontend. I am having challenge, when the page got loaded first time, java will return a jsp with list of items. I need to pass this list of items into React constructor for state initialize.
How to pass the list to React constructor?
Thanks!!!
The following are my program:-
import React from 'react';
import ReactDOM from 'react-dom';
class ItemList extends React.Component {
constructor(props) {
super(props);
this.state = {
itemList: this.props.items;
};
}
render() {
return (
<div>
<table id="item-table" className="table table-hover">
<tbody>
{this.state.itemList.map((item, i) => <TableRow key = {i} item = {item} />)}
</tbody>
</table>
</div>
);
}
};
class TableRow extends React.Component {
render() {
return (
<tr>
<td>{this.props.item.name}</td>
<td>{this.props.item.no}</td>
<td>{this.props.item.amount}</td>
</tr>
);
}
}
export default ItemList;
ReactDOM.render(<ItemList/>, document.getElementById('item-list'));
ReactDOM.render(<ItemList items={yourItemsJsObject}/>, document.getElementById('item-list'));