In React, conditional rendering is a powerful way to dynamically control what gets displayed in the user interface based on certain conditions. Here are various techniques for implementing conditional rendering:
Using if Statements
You can use if statements inside the render method to conditionally render components. This approach is straightforward but can make the render method longer and harder to read.
import React from 'react';
class MyComponent extends React.Component {
render() {
const isLoggedIn = this.props.isLoggedIn;
if (isLoggedIn) {
return Welcome back!
;
} else {
return Please sign in.
;
}
}
}
Using Ternary Operator
The ternary operator provides a concise way to render components conditionally.
import React from 'react';
class MyComponent extends React.Component {
render() {
const isLoggedIn = this.props.isLoggedIn;
return (
{isLoggedIn ? Welcome back!
: Please sign in.
}
);
}
}
Using Logical && Operator
The logical && operator allows you to render a component only if the condition is true. If the condition is false, it will render nothing.
import React from 'react';
class MyComponent extends React.Component {
render() {
const isLoggedIn = this.props.isLoggedIn;
return (
{isLoggedIn && Welcome back!
}
);
}
}
Using Switch Case
For more complex conditions, you can use a switch statement to determine what to render.
import React from 'react';
class MyComponent extends React.Component {
render() {
const status = this.props.status;
switch (status) {
case 'loading':
return Loading...
;
case 'error':
return Error occurred!
;
case 'loaded':
return Data loaded successfully!
;
default:
return Unknown status
;
}
}
}
Inline Conditional Rendering
You can embed conditions directly within JSX, which is useful for simple conditions.
import React from 'react';
class MyComponent extends React.Component {
render() {
const isLoggedIn = this.props.isLoggedIn;
return (
{isLoggedIn ? (
Welcome back!
) : (
Please sign in.
)}
);
}
}
Conditional Rendering with Functions
Encapsulating conditional rendering logic inside functions can make your code more readable and organized.
import React from 'react';
class MyComponent extends React.Component {
renderGreeting(isLoggedIn) {
if (isLoggedIn) {
return Welcome back!
;
}
return Please sign in.
;
}
render() {
const isLoggedIn = this.props.isLoggedIn;
return (
{this.renderGreeting(isLoggedIn)}
);
}
}
Using Hooks for Conditional Rendering in Functional Components
In functional components, hooks such as useState and useEffect can be used to manage state and side effects, enabling conditional rendering.
import React, { useState } from 'react';
const MyComponent = () => {
const [isLoggedIn, setIsLoggedIn] = useState(false);
return (
{isLoggedIn ? (
Welcome back!
) : (
Please sign in.
)}
);
};
export default MyComponent;
Conditional Rendering with Fragments
React fragments (<>>) can be used to group multiple elements without adding extra nodes to the DOM, which is useful for conditional rendering involving multiple elements.
import React from 'react';
const MyComponent = ({ isLoggedIn }) => {
return (
<>
{isLoggedIn ? (
<>
Welcome back!
>
) : (
<>
Please sign in.
>
)}
>
);
};
export default MyComponent;