Today, I want to share a clean way to conditionally render child components while using React. It's very simple.
Live Demo:
https://codesandbox.io/s/if-component-demo-9iipl?file=/src/App.js
Before
function App(){
// ...
return <div>
{someCondition?
<SomeChildA>
<div>Some Contents</div>
<div>Some Contents</div>
<div>Some Contents</div>
</SomeChildA>
:
<SomeChildB>
<div>Some Contents</div>
<div>Some Contents</div>
<div>Some Contents</div>
</SomeChildB>}
</div>
}
After
function App(){
// ...
return <div>
<If condition={someCondition}>
<SomeChildA>
<div>Some Contents</div>
<div>Some Contents</div>
<div>Some Contents</div>
</SomeChildA>
</If>
<If condition={!someCondition}>
<SomeChildB>
<div>Some Contents</div>
<div>Some Contents</div>
<If condition={someOtherCondition}>
<NestExample/>
</If>
<div>Some Contents</div>
</SomeChildB>
</If>
</div>
}
<If/> Component
function If(props) {
return props.condition ? <>{props.children}</> : null;
}
Thanks for reading! Have a nice day!
Latest comments (20)
We are a top-notch IGO Platform Development Company offering the industry best IGO development services for game developers and gaming enthusiasts to build community.
I wanted to add the same comment about the content of the
<If>being evaluated immediatelyand got some quite surprising results.
See here: codesandbox.io/s/if-component-b5zxk
Yes, a good example of how it works
Your idea is nice. However I think having an component would be overkill. Since the embed conditional rendering is already good.
Iβd like to add a bit. Since you want to render the ComponentX only with some conditions, itβd be better if you could lazy load it. So it will be downloaded only when we need, therefore downsize the initial bundle. Lazy and Suspense can help you to do it.
Just out of curiosity: do you really think that evaluation of a control-flow-like one-liner might turn into a bottleneck in a react app?
I mean, I'm agree that this approach might be error prone if one works with deeply nested & all-levels fully optional objects/arrays (which isn't the most common use case in the world by the way) but do you think it may anyhow affect the rendering performance?
I mean, it seems unlikely that someone would just accidentially stitch in all DOOM engine logic re-written & adapted for JS in-between the
<If></If>, isn't it?I just don't like the {...} things inside my JSX, it just doesn't look nice in terms of format.
IMO, the best way of doing that is this pattern
Much more concise, decently readable and short circuit saves some resources and possible runtime errors with not evaluating jsx.
Yes, it will cause an error. But it helps you write code in a more clean way. For example:
Since you will get an error by doing this way.
It forced you to do things like this which results a cleaner code:
Also, you can always check each property, but that is not recommended
thanks. this opened my eyes
I agree with @lukeshiru - This solution is really expensive. If you want to do this I would highly suggest you use a render prop.
Thanks for the feedback.
I don't think is good idea to put heavy task in the render return anyway so the performance does not suffer much by having a If wrappers.
If you have a case that may cause performance issue, please let me know
Yes, the statement in
<If/>will be evaluated, but I feel it is minor problem that can be avoid, for example, enclosing the statement inside the child component.Thanks for point this out!
Beside the performance issue, this can be an issue if you check in the condition a precondition for the rendering (e.g. if an object exists), and in the rendering part you depend on this check (e.g. access properties of this object).
E.g.
let post: Post | undefinedCheck in the condition if
post !== undefinedRender
post.titleif the condition is true