reactjs - React - How to pass data from html to React constructor -
i learning react , playing around it. existing project using java backend , planning integrate react @ frontend. having challenge, when page got loaded first time, java return jsp list of items. need pass list of items react constructor state initialize.
how pass list react constructor?
thanks!!!
the following program:-
import react 'react'; import reactdom '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'));
Comments
Post a Comment