Skip to content

Commit 1a79026

Browse files
committed
Merge branch '2.0-cn' of github.com:vuefe/vuejs.org into 2.0-cn
2 parents b5b30d2 + 2fea33d commit 1a79026

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/guide/comparison.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,38 @@ In React, everything is Just JavaScript, which sounds very simple and elegant -
5050

5151
#### JSX vs Templates
5252

53-
With React's JSX, there are minor differences from HTML, such as using `className` instead of `class`, but that's really not too bad. What feels really awkward is constantly switching back and forth between JavaScript and JSX, especially since not everything is an expression in JavaScript.
54-
55-
Take this example below:
53+
Let's dive into a simple React render function, kindly [provided by Dan Abramov](https://github.com/vuejs/vuejs.org/pull/371):
5654

5755
``` jsx
5856
render () {
59-
const { items } = this.props
57+
let { items } = this.props
58+
59+
let children
60+
if (items.length > 0) {
61+
children = items.map(item =>
62+
<li key={item.id}>{item.name}</li>
63+
)
64+
} else {
65+
children = <p>No items found.</p>
66+
}
6067

6168
return (
6269
<div className='list-container'>
63-
{items.length
64-
? <ul>
65-
{items.map(item => <li key={item.id}>{item.name}</li>)}
66-
</ul>
67-
: <p>No items found.</p>
68-
}
70+
{children}
6971
</div>
7072
)
7173
}
7274
```
7375

7476
This is an extremely common use case, but using JSX, there are a few problems that may not be immediately obvious:
7577

76-
- __Syntax Noise__: All the curly braces add visual noise, but what's worse is that while writing it, you have to frequently make decisions about how to minimize their visual impact, slowing development.
78+
- __Decisions, Decisions__: Despite the simplicity of the above code, it actually went through a series of iterations until Dan Abramov kindly ended the bikeshedding with a definitive version we could settle on. It's interesting to note though, that none of the other recommended solutions we saw were identical to Dan's. This is because JSX requires frequent compromises between readability and declarativeness that come down to individual judgment calls. These are some of the factors to be considered:
7779

78-
- __Cajoling JavaScript into Expressions__: The example uses a ternary rather than an if statement here, which arguably makes the code less readable, but is still possibly the best of bad options. The only alternatives include hoisting this logic out of its context or wrapping an if statement in an immediately-invoked function expression. Do expressions will help a little when they're a more stable feature, but they're currently at stage 0.
80+
- Not everything is an expression in JavaScript, making some common language features such as if statements a bit awkward to embed within JSX. There are sometimes alternatives which _are_ expressions, such as a ternary in this case, but many consider ternaries to be less readable.
81+
- When nesting JSX within JavaScript within JSX (etc), visual noise is created with curly braces and possibly parentheses. Different people make different decisions in how they prefer to minimize that noise.
82+
- To address the above two concerns, many choose to do as much as possible outside JSX, then only embed a variable, like `children` in the example above. The downside is that the render function then ceases to be a declarative description of the generated view, instead becoming an imperative list of operations with a final result.
7983

80-
- __Readability to Non-React Developers__: Imagine having a designer that's familiar with HTML and CSS look at this. They'd be paralyzed with confusion at every single line. While the JSX will look somewhat similar to HTML, `className` replaces `class`, the ternary will be meaningless, and parentheses and curly braces are everywhere. It'd be difficult for them to make simple modifications or even understand what this code is doing.
84+
- __Learning Curve__: While JSX looks similar to HTML, it's definitely not and there are edge cases to keep in mind - one being the use of `className` instead of `class`, such as in the example. To get this code reading as nicely as possible, it's also necessary to use ES2015+ language features, which many developers have not mastered yet. Combined, these two caveats significantly limit pool of people that can contribute to the frontend. You eliminate designers, intermediate JavaScript developers, or even advanced JavaScript developers that aren't yet familiar with the quirks of React's JSX.
8185

8286
In Vue, we also have [render functions](render-function.html) and even [support JSX](render-function.html#JSX), because sometimes it is useful to have the power of a full programming language. Render functions are not recommended for most components however.
8387

@@ -91,25 +95,32 @@ Instead, we offer templates as a simpler alternative:
9195
{{ item.name }}
9296
</li>
9397
</ul>
94-
<p v-else>No items found</p>
98+
<p v-else>No items found.</p>
9599
</div>
96100
</template>
97101
```
98102

99103
A few advantages here:
100104

101-
- Any valid HTML is valid in a template
102105
- Many fewer implementation and stylistic decisions have to be made while writing a template
103-
- There's much less visual noise
106+
- A template will always be declarative
107+
- Any valid HTML is valid in a template
104108
- It reads more like English (e.g. for each item in items)
109+
- Advanced versions of JavaScript are not required to increase readability
105110

106-
This is not only much easier for the developer that's writing it, but designers, junior developers, or those newer to JavaScript are also much more likely to be able to understand what's going on and even contribute code.
111+
This is not only much easier for the developer that's writing it, but designers and less experienced developers will also find it much easier parsing and contributing code.
107112

108113
It doesn't end there though. By embracing HTML rather than trying to reinvent it, Vue also allows you to use preprocessors such as Pug (formerly known as Jade) in your templates.
109114

115+
The React ecosystem also has [a project](https://wix.github.io/react-templates/) that allows you to write templates, but there are a few disadvantages:
116+
117+
- It's not nearly as feature-rich as Vue's templates
118+
- It requires separating your HTML from component files
119+
- Because it's a 3rd party library rather than an officially supported first-class citizen, it may or may not be kept up-to-date with React core into the future
120+
110121
#### Component-Scoped CSS
111122

112-
Unless you're OK spreading components out over multiple files (for example with [CSS Modules](https://github.com/gajus/react-css-modules)), it's difficult to find a good solution for scoping CSS in React. Very basic CSS works fine, but common features such as hover states, media queries, and pseudo-selectors all either require heavy dependencies to reinvent what CSS already does - or they simply don't work. And no matter what you end up using in the end, it'll have involved a lot of research before you can even get a simple hover state to work.
123+
Unless you spread components out over multiple files (for example with [CSS Modules](https://github.com/gajus/react-css-modules)), scoping CSS in React comes with caveats. Very basic CSS works great out-of-the-box, but some more complex features such as hover states, media queries, and pseudo-selectors all either require heavy dependencies to reinvent what CSS already does - or they simply don't work.
113124

114125
Vue on the other hand, gives you full access to CSS within [single-file components](single-file-components.html):
115126

@@ -125,7 +136,7 @@ Vue on the other hand, gives you full access to CSS within [single-file componen
125136

126137
The optional `scoped` attribute automatically scopes this CSS to your component by adding a unique attribute (such as `data-v-1`) to elements and compiling `.list-container:hover` to something like `.list-container[data-v-1]:hover`.
127138

128-
Finally, just as with HTML, you also have the option of writing your CSS using any preprocessors you'd like. This allows you to perform design-centric operations such as color manipulation during your build process, rather than importing specialized JavaScript libraries that would increase the size of your build and complexity of your application.
139+
Finally, just as with HTML, you also have the option of writing your CSS using any preprocessors (or postprocessors) you'd like. This allows you to perform design-centric operations such as color manipulation during your build process, rather than importing specialized JavaScript libraries that would increase the size of your build and complexity of your application.
129140

130141
### Scale
131142

0 commit comments

Comments
 (0)