Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression Expected in React using Switch Statement

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>
);
like image 788
JokerBean Avatar asked Mar 19 '19 09:03

JokerBean


People also ask

Can I use switch statement in React?

In React, a switch statement is one of the best ways to handle conditional rendering. For instance, you might want to render a specific component based on users' inputs. You can store the value of input fields in the state and examine the state value to determine the right component to render.

What is expression expected in JavaScript?

The "Expression expected" TypeScript error occurs when we have a syntax error in our code or our code editor is using an older version of TypeScript. To solve the error, make sure to correct and syntax errors and use a recent version of the TypeScript compiler.

What does 3 dots mean in React?

(three dots in JavaScript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed. This is not specific to React. It is a JavaScript operator.

How do you write if condition in JSX?

We can embed any JavaScript expression in JSX by wrapping it in curly braces. But only expressions not statements, means directly we can not put any statement (if-else/switch/for) inside JSX.


2 Answers

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>
);
like image 148
Ben Fortune Avatar answered Sep 23 '22 00:09

Ben Fortune


You have to use your switch statement in a function. Also, for clarity sake, you would be better off trying to keep conditional logic like that outside of your immediate component body.

export default function({ handle, state }) {
  function renderSwitch(item) {
    switch (item.name) {
      case "name1":
        return <InputBox />
      case "name2":
        return <SelectBox />
      case "name3":
        return <SelectBox />
      default:
        return <InputBox />
    }
  }

  return (
    <div style={styles.container}>
      <h3 style={{ margin: 0, marginBottom: 15 }}>InputData</h3>
      {items && items.map(item => <div style={styles.lineContainer}>{renderSwitch(item)}</div>)}
    </div>
  )
}
like image 22
Tom Finney Avatar answered Sep 23 '22 00:09

Tom Finney