I have a fetch call function that handles with a BackEnd. This call takes some time (couple of seconds).
There is also a table class that expect the data from the BackEnd to fill some of the rows. I am passing the function that does the fetch call via props.This function returns a list of values which the table class needs in order to fill some of the rows. Though, because the fetch call takes some time it seems like the value of the list in the table class side is always 'undefined' and some of the table rows that expect those values stays empty after the much quicker render method doing it's job..
I am using the "material-ui" npm package in order to build the table.
This is the fetch function:
var reuslts = [];
async callBackEnd() {
await fetch('*******', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify({
request: {
Purpose: "Ring",
price: "2000",
shape: "Round"
}
})
})
.then(r => r.json())
.then(r => {
r.forEach(function (element) {
reuslts.push('NO: ' + element);
});
});
console.log("I am near the end of app.js and the first value of the list is " + diamondReuslts[0]);
return reuslts;
}
This is where I pass the function as props:
render() {
const { title } = this.context;
return (
<div className="center-screen">
{title} <Table intents={intents} callBackEnd={this.callBackEnd()} />
</div>
);
}
This is the render method of the table class :
const SimpleTable = (props) => {
const { classes } = props;
intents1 = props.intents;
reuslts1 = props.callBackEnd;
console.log("I am at the beginning of Table.js and first value of the list is " + diamondReuslts1[0]);//Returns undefined
return (
<Paper className={classes.root}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<CustomTableCell>Image</CustomTableCell>
<CustomTableCell align="right">Price</CustomTableCell>
<CustomTableCell align="right">Id reference</CustomTableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => (
<TableRow key={row.id}>
<CustomTableCell component="th" scope="row">
<img src={row.feature} height="42" width="42"></img>
</CustomTableCell>
<CustomTableCell align="left">{intents1[row.id]}</CustomTableCell>
<CustomTableCell align="left">{reuslts1[row.id]}</CustomTableCell>
</TableRow>
))}
</TableBody>
</Table>
</Paper>
);
}
In this class the "results1" variable is undefined. When longing the returned value of Results from the callBackEnd function the value and message returned only long after the log of the table class returns "undefined".
What can I do in order to make the part of "rendering the table" wait or rerender when the list is returned from the BackEnd ?