Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is conditionally rendering a React component instead of raw markup save performance?

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?

like image 595
Yuriy Lemberg Avatar asked Mar 20 '18 00:03

Yuriy Lemberg


People also ask

Is used for conditionally rendering a React component?

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.

What is conditional rendering React?

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.

How do you hide a component from getting rendered with conditional rendering?

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.

What does ReactDOM render do?

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.


2 Answers

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.

like image 171
Estus Flask Avatar answered Sep 30 '22 04:09

Estus Flask


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.

like image 22
Jim Perris Avatar answered Sep 30 '22 04:09

Jim Perris