I have my ReactApp created and would like to display a table with data from AWS dynamodb.
I successfully got data from dynamodb transferred to ReactApp with json, through map() in render() of ReactApp, I displayed the table as below:
So now i need to make the table like re-distributed as below:
So I plan to render the table with if-else statement accordingly: but once i put in if-else clause in the function, it didn't work and nothing displayed. So would like to seek advices from experts in stackoverflow :) many thanks in advance
Here is my code, the part I'm stuck is in the return section in render():
class App extends Component {
constructor(props) {
super(props);
this.state = {
hits: [],
isLoading: false,
error: null,
};
}
render() {
const { hits, isLoading, error } = this.state;
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <p>Loading ...</p>;
}
return (
<form>
<div className="container border border-secondary rounded center">
<div className="row">
<div className="col-12">
{' '}
<h4>
<b>Class Mapping</b>
</h4>{' '}
</div>
</div>
<div className=".col-xs-12 center text-center">
<Table responsive striped bordered hover>
<tr>
<th>Class 1</th>
<th>Class 2</th>
<th>Class 3</th>
<th>Class 4</th>
<th>Class 5</th>
</tr>
<tbody>
{hits.map((hit) => {
if (hit.class === 'Class 1') {
<tr>
<td>{hit.name}}</td> <td></td> <td></td> <td></td> <td></td>{' '}
</tr>;
} else if (hit.class === 'Class 2') {
<tr>
<td></td>
<td>{hit.name}}</td> <td></td> <td></td> <td></td>{' '}
</tr>;
}
})}
</tbody>
</Table>
</div>
</div>
</form>
);
}
async componentDidMount() {
this.setState({ isLoading: true });
const response = await fetch('https://xxxxxxx');
const body = await response.json();
this.setState({ hits: body, isLoading: false, error: null });
}
}
export default App;