Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  133] [ 5]  / answers: 1 / hits: 28367  / 5 Years ago, tue, march 19, 2019, 12:00:00

I am using switch statement inside a react file .Getting Expression Expected error in first case line.



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

More From » reactjs

 Answers
44

If you want to inline a switch statement, you need to encase it inside an 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>
);

[#52404] Tuesday, March 12, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;