Skip to content

Commit a2c345d

Browse files
committed
update react comparison after discussion with dan
1 parent 0427593 commit a2c345d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/guide/comparison.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ render () {
5858

5959
let children
6060
if (items.length > 0) {
61-
children = items.map(item =>
62-
<li key={item.id}>{item.name}</li>
61+
children = (
62+
<ul>
63+
{items.map(item =>
64+
<li key={item.id}>{item.name}</li>
65+
)}
66+
</ul>
6367
)
6468
} else {
6569
children = <p>No items found.</p>
@@ -75,13 +79,15 @@ render () {
7579

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

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:
82+
- __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, just like JavaScript, requires frequent compromises between readability and declarativeness that come down to individual judgment calls. These are some of the factors to be considered:
7983

8084
- 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.
85+
- When nesting JSX within JavaScript within JSX (etc), visual noise is created with curly braces and often parentheses. Different people make different decisions in how they prefer to minimize that noise.
86+
- To address the above two concerns, the simplest option is 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 purely declarative description of the generated view, instead becoming an imperative list of operations with a declarative (but less descriptive) result. Other alternatives include:
87+
- Generating parts of the UI in helper functions, then calling them in the main render function. The downside is that visually parsing a component takes longer, as one has to jump around a lot more.
88+
- Breaking a component out into more, smaller components (e.g. `ListContainer`, `List`, `ListItem`, and `EmptyList`). This has advantages beyond making render functions easier to read as an application grows in complexity. However, if it never grows complex enough to otherwise warrant the split, it's a lot of added boilerplate.
8389

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.
90+
- __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 the 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.
8591

8692
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.
8793

0 commit comments

Comments
 (0)