gpt4 book ai didi

javascript - 使用 Switch 语句在 React 中预期的表达式

转载 作者:行者123 更新时间:2023-12-03 15:16:01 24 4
gpt4 key购买 nike

我在 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>
);

关于javascript - 使用 Switch 语句在 React 中预期的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55237619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com