You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/comparison.md
+19-12
Original file line number
Diff line number
Diff line change
@@ -50,12 +50,10 @@ In React, everything is Just JavaScript, which sounds very simple and elegant -
50
50
51
51
#### JSX vs Templates
52
52
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):
56
54
57
55
```jsx
58
-
render() {
56
+
render() {
59
57
let { items } =this.props
60
58
61
59
let children
@@ -66,7 +64,7 @@ render() {
66
64
} else {
67
65
children =<p>No items found.</p>
68
66
}
69
-
67
+
70
68
return (
71
69
<div className='list-container'>
72
70
{children}
@@ -77,11 +75,13 @@ render() {
77
75
78
76
This is an extremely common use case, but using JSX, there are a few problems that may not be immediately obvious:
79
77
80
-
-__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:
81
79
82
-
-__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.
83
83
84
-
-__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.
85
85
86
86
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.
87
87
@@ -95,22 +95,29 @@ Instead, we offer templates as a simpler alternative:
95
95
{{ item.name }}
96
96
</li>
97
97
</ul>
98
-
<pv-else>No items found</p>
98
+
<pv-else>No items found.</p>
99
99
</div>
100
100
</template>
101
101
```
102
102
103
103
A few advantages here:
104
104
105
-
- Any valid HTML is valid in a template
106
105
- Many fewer implementation and stylistic decisions have to be made while writing a template
107
-
- There's much less visual noise
106
+
- A template will always be declarative
107
+
- Any valid HTML is valid in a template
108
108
- It reads more like English (e.g. for each item in items)
109
+
- Advanced versions of JavaScript are not required to increase readability
109
110
110
-
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.
111
112
112
113
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.
113
114
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
+
114
121
#### Component-Scoped CSS
115
122
116
123
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.
0 commit comments