Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: Implement ng-if / v-if equivalent #20812

Closed
olivierr91 opened this issue Feb 12, 2021 · 3 comments
Closed

Feature suggestion: Implement ng-if / v-if equivalent #20812

olivierr91 opened this issue Feb 12, 2021 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@olivierr91
Copy link

olivierr91 commented Feb 12, 2021

I have worked with VueJS, Angular and now starting to write in React, and I find the conditional rendering syntax of React to be the worst. It is the least concise and least readable of all three syntax.

In Vue (and Angular) you can do conditional rendering using an extremely clean syntax:

<div v-if="var1 == true">Test1</div>
<div v-else-if="var2 == true">Test2</div>
<div v-else-if="var3 == true">Test3</div>
<div v-else>Test4</div>

In React, to get the same result, you have to write some ugly syntax like this:

{var1 == true && <div>Test1</div>}
{var1 == false && var2 == true && <div>Test2</div>}
{var1 == false && var2 == false && var3 == true && <div>Test3</div>}
{var1 == false && var2 == false && var3 == false && <div>Test4</div>}

which has some unnecessary repeating logic. You can also write nested ternary operations:

{var1 == true ? <div>Test1</div> : 
    (var2 == true ? <div>Test2</div> :
        (var3 == true ? <div>Test3</div> :
            <div>Test4</div>))}

which is still pretty ugly and will get even more ugly with more complex DOM nodes. You can also write:

if (var1 == true) {
    return <div>Test1</div>
} else if (var2 == true) {
    return <div>Test2</div>
} else if (var3 == true) {
    return <div>Test3</div>
} else {
    return <div>Test4</div>
}

but this code cannot be put inside another JSX element. I know there are libraries out there that can mimic the Vue syntax but this should be implemented in React natively, and I don't think this would be hard to implement at the JSX to JS transform level.

@olivierr91 olivierr91 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 12, 2021
@gaearon
Copy link
Collaborator

gaearon commented Feb 12, 2021

Hi there!

It is a fully intentional design decision that you'd use regular JavaScript for conditions instead of a template syntax, and this is not something we intend to change.

Here is how I'd write your example:

let child;
if (var1) {
  child = <div>Test1</div>
} else if (var2) {
  child = <div>Test2</div>
} else if (var3) {
  child = <div>Test3</div>
} else {
  child = <div>Test4</div>
}

return (
  <div>
    {child}
  </div>
);

The == true comparisons are not necessary, and by using a variable you can put the result inside of JSX.

Hope this is reasonable.

@gaearon gaearon closed this as completed Feb 12, 2021
@olivierr91
Copy link
Author

Vue/Angular is still cleaner than your example and way less code. And yet this is a simple example. If I had nested conditions inside each of those divs this is going to be a hard-to-understand mess. Writing/reading conditional rendering statements along the flow of the DOM is way simpler than extracting that logic at the top. If you need to write more code with a Framework B to achieve the same logic as Framework A than Framework A has a better design. Fact is, Vue/Angular have much better and cleaner conditional rendering design. So no, I don't think this is reasonable, but I know I'm not going to change your mind.

@gaearon
Copy link
Collaborator

gaearon commented Feb 12, 2021

"Cleaner" is very subjective, as is the claim that less code always indicates a better design. We don't mind some verbosity. React's popularity speaks to the fact that many people find the approach without special syntax cleaner, in a different way.

In longer term it is plausible that we might introduce some nicer syntax to do conditions in the middle of the tree. However, such syntax would be based on JavaScript too (e.g. facebook/jsx#88) rather than on adding a custom attribute syntax and overloading it with semantic meaning.

We don't want to introduce mechanisms other than the ones that already exist in the language. If you find them acceptable for writing JavaScript logic, React asks that you apply the same mindset to your markup. I can totally understand the stylistic preference though, and you're welcome to use other libraries if you strongly disagree with this choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants