I have a ponent that needs to render by condition, I'm confused between the two ways like the code below and I am figuring out the remended way of React. What is the best way to set render conditions for a ponent? I'm new to React and would love some help.
The first way:
const MyComponent1 = () => {
return (
<div>This is my ponent 1</div>
)
}
{isRender && <MyComponent />}
The second way:
const MyComponent2 = ({ isRender }) => {
if (!isRender) return null;
return (
<div>This is my ponent 2</div>
)
}
<MyComponent isRender/>
I feel the second approach will redundancy the logic code inside this ponent if the ponent is not rendered (e.g: hooks, handle function, ...) The first way will not affect the CPU because the ponent is not rendered so the internal logic will not be executed. Is that true?
I have a ponent that needs to render by condition, I'm confused between the two ways like the code below and I am figuring out the remended way of React. What is the best way to set render conditions for a ponent? I'm new to React and would love some help.
The first way:
const MyComponent1 = () => {
return (
<div>This is my ponent 1</div>
)
}
{isRender && <MyComponent />}
The second way:
const MyComponent2 = ({ isRender }) => {
if (!isRender) return null;
return (
<div>This is my ponent 2</div>
)
}
<MyComponent isRender/>
I feel the second approach will redundancy the logic code inside this ponent if the ponent is not rendered (e.g: hooks, handle function, ...) The first way will not affect the CPU because the ponent is not rendered so the internal logic will not be executed. Is that true?
Share Improve this question edited Dec 21, 2024 at 10:39 Mayank Kumar Chaudhari 18.6k13 gold badges67 silver badges151 bronze badges asked Jul 21, 2023 at 5:32 EppleEpple 9721 gold badge12 silver badges33 bronze badges 2- second approach is better, it will not break your app later – Medet Tleukabiluly Commented Jul 21, 2023 at 5:34
-
@MedetTleukabiluly Second approach is not remended because a ponent returning
null
is pretty unexpected and bad practice as it makes things harder to debug. A ponent should always render something and the conditional rendering logic should remain in the parent. – xyres Commented Jul 21, 2024 at 4:54
2 Answers
Reset to default 7The first way is much better. As you rightly pointed out, you need to keep all hooks before the first return statement. You will be unnecessarily adding those hooks and any other logic when you don't need to render the ponent.
Secondly, and more importantly, while using the first way you can use lazy loading to only load the ponent when it needs to be rendered for the first time and thereby improving the performance.
among the most mon strategies are:
Employing the if/else or ternary operator:
To conditionally render ponents, you can use either a standard if/else expression or a ternary operator. Here is an illustration of the ternary operator in use:
function MyComponent({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? <LoggedInComponent /> : <LoggedOutComponent />}
</div>
);
}
Using && operator: The && operator can be used for simple conditional rendering when you want to render a ponent based on a single condition:
function MyComponent({ isLoading }) {
return (
<div>
{isLoading && <LoadingComponent />}
</div>
);
}
Using switch/case statement: If you have multiple conditions to handle, you can use a switch statement:
function MyComponent({ status }) {
switch (status) {
case 'success':
return <SuccessComponent />;
case 'error':
return <ErrorComponent />;
default:
return <DefaultComponent />;
}
}
Using a function for conditional rendering: You can define a separate function to decide which ponent to render:
function MyComponent({ condition }) {
function getComponent() {
if (condition === 'A') {
return <ComponentA />;
} else if (condition === 'B') {
return <ComponentB />;
} else {
return <DefaultComponent />;
}
}
return (
<div>
{getComponent()}
</div>
);
}
Select a strategy based on your unique use case and coding preferences. Utilising tools like React Router or Redux in some circumstances could also offer more effective solutions to manage plex conditional rendering scenarios. When selecting a method, always keep readability and maintainability of your code in mind.