I am trying to render a table in which each row (1,2,3) has expandable rows (a,b,c). Row will get data from API/rows, expandable rows will get data from API/rowId?expandableRows
. expandable rows will follow it's row when rendered like this:
row col1 col2 col3
1 x x x
a x x x
b x x x
2 x x x
a x x x
3 x x x
a x x x
Here is my code:
class Table extends Component {
constructor(){
super();
this.state={ rowList: [] }
}
componentDidMount() {
fetch(API/rows)
.then(results=>{return results.json()})
.then(rows =>{
let rowList= rows.map(row=> {
return (
<react.Fragment>
<tr><td>{row.info}</td></tr>
{this.getExpandableRow(row.id).then(exrows=>{return exrows})}
</react.Fragment>
)
})
this.setState(rowList: rowList);
})
}
getExpandableRow(id) {
return fetch(api/rowid?expandableRows)
.then(results=>{return results.json();})
.then(expandData=>{
data.map(dat=>{
return (
<tr><td>expandrow.info</td></tr>
)
})
console.log(expandData)
})
}
render(){
return (<Table>{this.state.rowList}</Table>)
}
}
Now the problem is I can see expand data in console.log
, but when calling getExpandableRows
function in rows loop, no matter use {this.getExpandableRow(row.id)}
or {this.getExpandableRow(row.id).then(exrows=>{return exrows})}
, I got the error:
Objects are not valid as react child(found:[object promise]), if you meant to render a collection of children, use an array instead.
What should I do to get expandable row data when looping rows?