This will still render the components inside, so if those depend on the condition, you'll still get errors. Not to mention performance is worsen as well. You can verify this by adding a log to the components inside If and setting the condition to false.
Thanks for the feedback!
I'm not sure if I understand you reply correctly, but here is demo that shows the child component will not be rendered unless the condition is true.
I was talking about this ... with a ternary you don't run the code inside the "if", but with an If component you do, because the content of an If component will be evaluated. If you want to use JSX like this, you can use Solid JS which afaik allows you to do stuff like this ^_^
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.
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 | undefined
Check in the condition if post !== undefined
Render post.title if the condition is true
Nowadays you "solve" that with optional chaining, but yeah, the important thing here is that the content of If is evaluated, and that wouldn't happen with a ternary.
import{jsx}from"react/jsx-runtime";import{Fragment}from"react";constIf=({children,condition})=>condition?jsx(Fragment,{children}):undefined;constDisplayData=({data})=>jsx(Fragment,{children:data});constdata=undefined;// The important bit:exportdefaultjsx(If,{condition:data,children:jsx(DisplayData,{data}),});
import{jsx}from"react/jsx-runtime";import{Fragment}from"react";constDisplayData=({data})=>jsx(Fragment,{children:data});constdata=undefined;// The important bit:exportdefaultdata?jsx(DisplayData,{data}):undefined;
With If you always call jsx with DisplayData passing data, without If you only do that call if data exists, if not you don't do anything.
If you mean the render of <If/> component will be executed regardless of the condition, that is true. But I don't think it will cause any error itself.
For performance, so far I don't feel any drawback of using this approach since it does not render its children when condition if false as I mentioned previously.
It should be very similar to the <Route/> in React-Router.
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?
You don't have to take my word for this (even when I did this in the past, realized it was a mistake and stopped doing it), you can just google this "If" component approach. I'm not the only one that knows this isn't ideal because you're evaluating branches of logic even when you aren't actually going into them.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This will still render the components inside, so if those depend on the condition, you'll still get errors. Not to mention performance is worsen as well. You can verify this by adding a log to the components inside
Ifand setting the condition tofalse.Cheers!
Thanks for the feedback!
I'm not sure if I understand you reply correctly, but here is demo that shows the child component will not be rendered unless the condition is true.
codesandbox.io/s/if-component-demo...
Output
If you change the code to this:
You will get a error.
I was talking about this ... with a ternary you don't run the code inside the "if", but with an
Ifcomponent you do, because the content of anIfcomponent will be evaluated. If you want to use JSX like this, you can use Solid JS which afaik allows you to do stuff like this ^_^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 trueNowadays you "solve" that with optional chaining, but yeah, the important thing here is that the content of
Ifis evaluated, and that wouldn't happen with a ternary.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
Instead of having a component, you could have an util:
And then you use it like this:
Behaves similarly to your
Ifcomponent, but it only callsrenderifconditionis truthy, so there's a lot of issues you wouldn't have :DI just don't like the {...} things inside my JSX, it just doesn't look nice in terms of format.
JSX will always end up with
{...}somewhere. You can always use stuff like Svelte, that already includes "if" in their templating ☺️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
If you understand the underlying code for React, then this is no surprise. When you write:
You're actually writing this:
Compared to doing it with a ternary like this:
That's like writing this:
With
Ifyou always calljsxwithDisplayDatapassingdata, withoutIfyou only do that call ifdataexists, if not you don't do anything.If you mean the render of
<If/>component will be executed regardless of the condition, that is true. But I don't think it will cause any error itself.For performance, so far I don't feel any drawback of using this approach since it does not render its children when condition if false as I mentioned previously.
It should be very similar to the
<Route/>inReact-Router.Best
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?You don't have to take my word for this (even when I did this in the past, realized it was a mistake and stopped doing it), you can just google this "If" component approach. I'm not the only one that knows this isn't ideal because you're evaluating branches of logic even when you aren't actually going into them.