How to use switch statement inside a React component?

In this article, I would like to share with you the steps that “How to use switch statement inside a React component?” in your project file.

How to use switch statement inside a React component?

To use switch statement inside a React component, you can use one of two methods: either you can use the switch statement outside the render or put the switch inline with your code.

There are multiple ways to use switch statement inside a React component in your project file. You can follow one of the below-mentioned methods to use switch statement inside a React component:

Method 1: Put switch Statement out of the Render

To use switch statement inside a React component try this method which is one of the cleaner and easy to understand methods to use switch in react.

<switch> is unique in that it only renders a route. Every that matches the location, on the other hand, renders inclusively.

First you will have to get the switch statement out of the render and in a function. Then you have to just call it passing the parameters you want. For reference follow the below mentioned code:

renderSwitch(param) {
  switch(param) {
    case 'foo':
      return 'bar';
    default:
      return 'foo';
  }
}

render() {
  return (
    <div>
      <div>
          // removed for brevity
      </div>
      {this.renderSwitch(param)}
      <div>
          // removed for brevity
      </div>
    </div>
  );
}

Method 2: Putting the Switch Statement Inline

In contrast to the method mentioned above, you can also use the “switch” inside the render function, this is also one of the methods that is very easy to understand.

It makes it more clear what components can be rendered at that position. You would be able to implement a switch-like expression by using a plain old javascript object, by following the below mentioned code as a reference:

render () {
  return (
    <div>
      <div>
        {/* removed for brevity */}
      </div>
      {
        {
          'foo': <Foo />,
          'bar': <Bar />
        }[param]
      }
      <div>
        {/* removed for brevity */}
      </div>
    </div>
  )
}

There is one more way you could use switch statement inside a React component with the incline code method. The switch statement is a statement, but here javascript expects an expression.

Although, it is not recommended to use a switch statement in a render method, you can use a self-invoking function to achieve this, by following the below mentioned code as an example:

render() {
    // Don't forget to return a value in a switch statement
    return (
        <div>
            {(() => {
                switch(...) {}
            })()}
        </div>
    );
}

This should help you understand “How to use switch statement inside a React component”

Conclusion

To use switch statement inside a React component, you can use one of two methods; you can either use the switch statement outside the render or put the switch inline with your code.

Hope this article has helped you to clarify all possible methods that How you can use switch statement inside a React component.