Conditional rendering component using Enums in ReactJS
Last Updated :
30 Nov, 2023
In certain scenarios, a ReactJS developer may have to dynamically display or hide components depending on specific conditions. For instance, when creating a To-Do list application, the developer must render tasks if there are pending items; otherwise, they should display a message such as "No pending tasks available."
Prerequisites:
Steps to Create the React Application And Installing Module:
Step 1: To create a new React project, run the below command to your terminal.
npx create-react-app testapp
Step 2: To move inside the project directory, run the below command to your terminal.
cd testapp
Project Structure:

Rendering Component using enum:
Step 1: In javascript, we can create an object with key-value pairs and use it as an enum. Below, you can see the demo of a javascript object with key-value pair.
Syntax:
const Enumobj = {
key: value,
};
Example:
const Enumobj = {
first: <First />,
second: <Second />
};
Step 2: Now, we will make a javascript function that takes a state as a parameter and return a React component based on the state.
Syntax:
function Enum({state}){
return {object[state]};
}
Example:
function Enum({ state }) {
return <div>{Enumobj[state]}</div>;
}
Step 3: Let's embed the 'Enum' function in our 'App' component. While calling the 'Enum' function, we will add state value as props.Â
Syntax:
return (
<div>
<Enum state="Value"></Enum>
</div>
);
Example:
return (
<div>
<Enum state="first"></Enum>
<Enum state="second"></Enum>
</div>
);
Example: In this example,we will create an enum object first. After that, we will add an 'enum' function to render components according to state value. we will add some basic React code to render on the webpage. The user needs to add the following code to the 'first.js' file.
JavaScript
import React, { Component } from 'react';
import Second from './components/second'
import First from './components/first'
// Creating enum object
const Enumobj = {
first: <First />,
second: <Second />
};
// Creating enum function to return
// Particular component according to state value
function Enum({ state }) {
return <div>{Enumobj[state]}</div>;
}
// Call enum function inside the App component
class App extends Component {
render() {
return (
<div>
<Enum state="first"></Enum>
<Enum state="second"></Enum>
</div>
);
}
}
export default App;
JavaScript
import React, { Component } from 'react';
// Some basic code to render first component
class First extends Component {
render() {
return (
<div>
<h1 style={{ color: "green" }}>GeeksForGeeks</h1>
<h2>This is a first component</h2>
</div>
);
}
}
export default First;
JavaScript
import React, { Component } from 'react';
// some basic code to render second component
class Second extends Component {
render() {
return (
<div>
<h1 style={{ color: "green" }}>GeeksForGeeks</h1>
<h2>This is a second component.</h2>
</div>
);
}
}
export default Second;
Steps to Run: The user needs to run beneath the command to the terminal in the current directory to see the output.
npm start
Output:

Similar Reads
How to conditionally render components in ReactJS ? React is a declarative, efficient, and flexible JavaScript library for building user interfaces. Itâs âVâ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by Facebook.PrerequisitesNodeJS or NPMReact JSReact
3 min read
How to implement Conditional Rendering in React? This article explores JavaScript's conditional rendering in React JS, including various methods, syntax, and applications, leveraging React's virtual DOM, reusable components, props, and rapid rendering capabilities in single-page UI applications.We will use the following approaches to implement con
4 min read
React Conditional Rendering Conditional rendering allows dynamic control over which UI elements or content are displayed based on specific conditions. It is commonly used in programming to show or hide elements depending on user input, data states, or system status. This technique improves user experience by ensuring that only
6 min read
ReactJS Components Complete Reference In ReactJS, components are the building blocks that help you create interactive UIs by dividing your app into smaller, reusable pieces of code. Understanding how components work is essential for efficiently developing React applications. In this article will provide a complete reference to React com
3 min read
How to prevent a component from rendering ? In React JS, preventing a component from rendering simplifies to conditional rendering of the component. When UI is designed using React, we come across a situation when components are to be rendered on the screen based on some condition. For eg, In a University Information System, when a teacher lo
3 min read