-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Comments
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 Hope this is reasonable. |
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. |
"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. |
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:
In React, to get the same result, you have to write some ugly syntax like this:
which has some unnecessary repeating logic. You can also write nested ternary operations:
which is still pretty ugly and will get even more ugly with more complex DOM nodes. You can also write:
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.
The text was updated successfully, but these errors were encountered: