I would argue that evaluating the condition_1 condition twice is still way more efficient than the overhead produced by unnecessary React nodes for If, ElseIf, Else. All these components will be translated to React.createElement(component, props, ...children) calls.
Regardless of the condition, both the children of If and Else will be executed by JavaScript․ This can lead to unwanted side effects and performance issues. In such cases, we can pass a function instead of children.
@zirkelc Thanks for the question.
In practice everything is more complicated than the simple example you mentioned.
{props.condition_1 ? (props.condition_2 ? () : ()) : (props.condition_3 ? () : ())}
I see your point and agree that tenary expression can get quite complicated with multiple conditions.
In your example, I would create two separate if statements for
condition_2
andcondition_3
and include the firstcondition_1
in both cases:I would argue that evaluating the
condition_1
condition twice is still way more efficient than the overhead produced by unnecessary React nodes forIf
,ElseIf
,Else
. All these components will be translated toReact.createElement(component, props, ...children)
calls.@zirkelc You are right please see the solution:
react-comfort.js.org/docs/componen...
Regardless of the condition, both the children of
If
andElse
will be executed by JavaScript․ This can lead to unwanted side effects and performance issues. In such cases, we can pass a function instead of children.Let's consider the following example:
In the example above, the
getResult()
function will always be called regardless of whetherhasResult
is true or false.Let's solve that problem․
But why make it more complicated than it has to be?
@zirkelc to make it more readable ) We can also don't use JSX, but we use it to make code more readable and comfortable.