5

In my reactjs component - in the render, I want to create a switch case that will use particular components based on a switch case. But there is a syntax error here.

Should I just make this as a function inside the component?

Unexpected token (190:32)

  188 | 
  189 |   {
> 190 |     return (
      |     ^

Here is the full return function for the component:

return (
    <div className="Page">
        ....
    {
        elements.map(function(element, index){
            return (
                <Col xs={24} sm={element["grid-width"]*2} md={element["grid-width"]} key={index} className="element-container">
                  .....
                      <div className="contents">
                        {
                          return (
                            switch (element.type) { 
                              case 'we-are-always-here-to-help':
                                sss
                                break;
                              case 'call-us-when-you-are-having-difficulties':
                                xx
                                break;
                              default: 
                            }
                          )
                        }
                      </div>
                </Col>
            )
        })
    }
    </div>
)
2
  • can you post the full component code? Commented Aug 30, 2017 at 14:22
  • Difficult to read your code. Likely, you need a switch case where each case will return something, rather than the other way around Commented Aug 30, 2017 at 14:27

4 Answers 4

6

Rule:

We can put any expression not statement inside JSX by using {}.

It's not a valid syntax, we can not use switch statement or any other statement directly inside JSX.

Solution:

Put all the switch part logic inside a function, call that function from JSX and return the result.

Write it like this:

<div>
    {this._switchPart(element.type)}
</div>

Define _switchPart like this:

_switchPart(type){
    switch (type) {
       case 'we-are-always-here-to-help': return sss;

       case 'call-us-when-you-are-having-difficulties': return xx;

       default: return xy;
    } 
}

Or use ternary operator for conditional rendering or you can also use IIFE.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use an IIFE like so:

{(() => {
  switch (element.type) {
    case 'we-are-always-here-to-help':
      return sss;
      break;
    case 'call-us-when-you-are-having-difficulties':
      return xx;
      break;
    default:
      return dd;
  }
})()}

Comments

0

Working example for this issue:

class Example extends React.Component {
    constructor () {
        super();
    }

    renderView() {
        switch (this.props.viewName) {
            case 'index': {
                return (<div>Index</div>);
            }

            default: {
                return <div>404 Error</div>;
            }
        }
    }

    render () {
        return (<div>{this.renderView()}</div>);
    }
};

ReactDOM.render(<Example viewName="index" />, document.getElementById("root"));

Comments

0

You can use plain object instead of switch:

<div className="contents">
  {{
    'we-are-always-here-to-help': sss,
    'call-us-when-you-are-having-difficulties': xx,
  }[element.type]}
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.