This is something that makes sense in my head but I haven't been able to find any facts/articles to back this up.
Essentially is doing something like
render() {
return (
someBoolean && <div>Some Markup</div>
)
}
less performant than
render() {
return (
someBoolean && <SomeComponent />
)
}
where SomeComponent
has the same exact markup as the previous example.
My reasoning is since the markup will have to be created on every re-render it'll take up more memory whereas the saved component SomeComponent
will be referenced in memory and won't have to be created on every re-render.
Is there somewhere in the react docs that explains this more thoroughly?
Or is this reasoning not accurate?
Conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like if or the conditional operator to create elements representing the current state, and let React update the UI to match them. This example renders a different greeting depending on the value of isLoggedIn prop.
In React, we can create multiple components which encapsulate behavior that we need. After that, we can render them depending on some conditions or the state of our application. In other words, based on one or several conditions, a component decides which elements it will return.
If you want to hide a component, you can make its render method return null , so there's no need to render an empty, different element as a placeholder.
The ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.
JSX is syntactic sugar for React.createElement
. As it can be seen in Babel REPL, they are
return someBoolean && React.createElement(
"div",
null,
"Some Markup"
);
and
return someBoolean && React.createElement(SomeComponent, null);
respectively.
When someBoolean
is falsy, React.createElement
isn't called and render
becomes no-op.
SomeComponent
isn't cached, it is re-created every time. In comparison with raw markup, it provides negligible overhead, considering that it is a stateless component:
const SomeComponent = () => <div>Some Markup</div>;
Both options are very fast and aren't supposed to be optimized.
Whether it's conditional or not is not an issue, as neither will be evaluated past the &&
if someBoolean
is false.
The real question is whether a defined React subcomponent has a performance advantage or penalty versus plain JSX markup defined within another component. For practical purposes, there is no performance difference unless you are making use of custom functions for lifecycle hooks.
For example, if you use a subcomponent, you may define a separate shouldComponentUpdate
method within that component and thereby decouple its updates from the parent. In the case of plain JSX, it will update as part of the parent component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With