【发布时间】:2020-11-11 11:58:16
【问题描述】:
我创建了我的 ReactApp,并希望显示一个包含来自 AWS dynamodb 的数据的表格。
我成功的将dynamodb中的数据通过json传输到ReactApp,通过ReactApp的render()中的map(),我显示的表格如下:
所以现在我需要将表格重新分配如下:
所以我计划相应地使用 if-else 语句来呈现表格: 但是一旦我在函数中添加了 if-else 子句,它就不起作用并且什么也没有显示。所以想向stackoverflow专家寻求建议:)非常感谢提前
这是我的代码,我卡住的部分在 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;
【问题讨论】:
标签: reactjs dictionary jsx