javascript - 使用 Switch 语句在 React 中预期的表达式
问题描述
我在反应文件中使用 switch 语句。在第一个案例行中获取表达式预期错误。
export default ({handle, state}) => (
<div style={styles.container} >
<h3 style={{margin: 0, marginBottom: 15}}>InputData</h3>
{items.map((item) => (
<div style={styles.lineContainer}>
switch(item.name){
case "name1": return <InputBox/>;
case "name2": return <SelectBox/>;
case "name3": return <<SelectBox/>;/>;
default: return <InputBox/>
}
</div>
))}
</div>
);
解决方案
如果要内联 switch 语句,则需要将其封装在 IIFE 中。
export default ({handle, state}) => (
<div style={styles.container}>
<h3 style={{margin: 0, marginBottom: 15}}>InputData</h3>
{items.map((item) => (
<div style={styles.lineContainer}>
{(() => {
switch(item.name) {
case "name1": return <InputBox/>;
case "name2": return <SelectBox/>;
case "name3": return <SelectBox/>;
default: return <InputBox/>
}
})()}
</div>
))}
</div>
);
推荐阅读
- json - 在 Swift 中使用 Codable 进行解析
- python - 使用 python 和 AudioSegment 进行实时音频处理
- android - 房间不使用 @Update(onConflict = OnConflictStrategy.REPLACE) 更新实体
- vue.js - 如何在 Vuejs 中创建带有可拖动项目的嵌套列表?
- powershell - 向脚本添加 -WhatIf 或“仅记录”功能?
- delphi - 如何用信号通知 Indy TCPServer 连接线程终止?
- python - 传递非默认依赖项时,如何强制重新计算函数参数中的依赖参数值?
- haskell - 为什么 Haskell 不能优化这个重复的函数调用?
- google-apps-script - 过滤谷歌表中的复合列
- python - 错误:Django 框架:/accounts/create/'shop' 中的 NoReverseMatch 不是已注册的命名空间