javascript - ReactJS:在表单内的选择中显示列表
问题描述
我试图在表单中显示一个列表,但我不明白为什么它没有显示。
useEffect(() => {
props.getRealms(); // I make call to api
localStorage.setItem('realm', JSON.stringify(realm));
}, [realm]);
const { realmsList, loadingRealms } = props;
console.log('realmsList.length ', realmsList.length);
return (
<div>
<AvForm model={{}} onSubmit={saveEntity}>
{realmsList.length > 0 ? (
<div>
<AvGroup>
<Label>Realm</Label>
<AvInput id="personaInfo-realm" data-cy="realm" type="select" name="realm">
<option value="" key="0">
Choose Realm
</option>
{realmsList.map(entity => {
console.log('entity ', entity),
(
<option key={entity.id} value={entity.id}>
{entity.name}
</option>
);
})}
</AvInput>
</AvGroup>
<Button color="primary" id="save-entity" data-cy="entityCreateSaveButton" type="submit" style={{ marginLeft: 10 }}>
<FontAwesomeIcon icon="save" /> Save
</Button>
</div>
) : (
<div className="d-flex justify-content-center">
<div className="spinner-border text-primary" role="status">
<span className="visually-hidden"></span>
</div>
</div>
)}
</AvForm>
</div>
);
};
const mapStateToProps = ({ sceRealm }: IRootState) => (
{
realmsList: sceRealm.entities,
loadingRealms: sceRealm.loading,
}
);
const mapDispatchToProps = {
getRealms,
};
type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;
export default connect(mapStateToProps, mapDispatchToProps)(ScePersonaInfoUpdate);
在我的回报中,我写了这个条件:realmsList.length > 0,但我唯一能看到的是写“选择领域”而不是领域列表。地图内的控制台日志,显示列表的实体。
我该如何解决?
谢谢
解决方案
您的地图内没有返回,因此您的地图实际上没有发生任何事情
{realmsList.map(entity => {
console.log('entity ', entity),
return (
<option key={entity.id} value={entity.id}>
{entity.name}
</option>
);
})}
推荐阅读
- oracle - PL/SQL: ORA-44201: 游标需要重新解析
- python - python manage.py migrate 分段错误:11
- ios - 如何在谷歌地图中快速创建标记的自定义视图
- python - Umat 很慢(OpenCV、Python)
- javascript - 移动不同范围滑块时动态更改范围滑块值
- multithreading - 16个线程等待同一个锁
- android - 如何在Android中将变量从一个类传递到另一个类
- python - 使用python检测图像中的牙齿缩小
- html - 文本输入将焦点移出其边界
- go - 为什么使用标志“-race”的结果不符合预期?