close

How to use a switch Case statement inside a React component?

Hello Guys How are You all? Hope You all Are fine. Do you need to Use Switch Case Statement inside React Component? Today in this tutorial we are going to learn How to use a switch Case statement inside a React component? so lets start this tutorial without wasting your time.

How to use a switch Case statement inside a React component?

  1. How to use a switch Case statement inside a React component?

    To use a switch Case statement inside a React component we are Going to Use Function. We will Make a One Function with a Switch case Statement. I am making function named manageUsers() and I will pass the role into it. From that role We will make a Switch case If the role is admin then it will pass Admin Like that. Here is the mY Function. Now I am going to Render this Function in My code Now, I am Just using mu Function in my H1 and I can Use the Switch case Statement in my app.

  2. switch Case statement inside a React component

    To use a switch Case statement inside a React component we are Going to Use Function. We will Make a One Function with a Switch case Statement. I am making function named manageUsers() and I will pass the role into it. From that role We will make a Switch case If the role is admin then it will pass Admin Like that. Here is the mY Function. Now I am going to Render this Function in My code Now, I am Just using mu Function in my H1 and I can Use the Switch case Statement in my app.

Solution 1: Using function

In This Solution, we are Going to Use Function. We will Make a One Function with a Switch case Statement. I am making function named manageUsers() and I will pass the role into it. From that role We will make a Switch case If the role is admin then it will pass Admin Like that. Here is the mY Function.

 manageUsers(role) {

    switch(role) {
      case 'Admin':
        return 'You are a Admin.';
      case 'Editor':
        return 'You are a Editor.';
      case 'Maintainer':
        return 'You are a Maintainer.';
      default:
        return 'You are a User';
    }

  }

Now I am going to Render this Function in My code

import logo from "./logo.svg";
import "./App.css";

function App() {

  function manageUsers(role) {
    switch (role) {
      case "Admin":
        return "You are a Admin.";
      case "Editor":
        return "You are a Editor.";
      case "Maintainer":
        return "You are a Maintainer.";
      default:
        return "You are a User";
    }
  }

  return (
    <div className="App">
      <h1>{manageUsers("admin")}</h1>
    </div>
  );

}

export default App;

Now, I am Just using mu Function in my H1 and I can Use the Switch case Statement in my app.

Solution 2: You can Directly use Switch Case in Render

If You Don’t want to make a Function then you can also use the Switch case directly in your render(). You have to make the Const variable with Role and then pass Role into it and then switch case will use it. Just like this.

import logo from "./logo.svg";
import "./App.css";

function App() {
  const role = 'Editor';

  return (
    <div className="App">
      <h1>Welcome to Exerror.com</h1>
      <h2>
        {() => {
          switch (role) {
            case "Admin":
              return "You are a Admin.";
            case "Editor":
              return "You are a Editor.";
            case "Maintainer":
              return "You are a Maintainer.";
            default:
              return "You are a User";
          }
        }}
      </h2>
    </div>
  );
}

export default App;

Now you can easily use a switch case statement. Thank you.

Conclusion

It’s all About How to use a switch Case statement inside a React component?. I hope You understand how to use it. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also, Read

Leave a Comment