-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.6.tsx
199 lines (166 loc) · 6.1 KB
/
1.6.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***************** Conditional Rendering *********************/
/**
* - Your components will often need to display different things depending on different conditions.
* In React, you can conditionally render JSX using JavaScript syntax like if statements, &&,
* and ? : operators.
*/
/******* Conditional returning JSX ********/
/**
*
* - Notice that some of the Item components have their isPacked prop set to true instead of false.
* You want to add a checkmark (✔) to packed items if isPacked={true}.
* You can write this as an if/else statement like so:
if (isPacked) {
return <li className="item">{name} ✔</li>;
}
return <li className="item">{name}</li>;
*/
/**
* Conditionally returning nothing with null
*
* - In some situations, you won’t want to render anything at all. For example, say you don’t
* want to show packed items at all. A component must return something. In this case,
* you can return null:
if (isPacked) {
return null;
}
return <li className="item">{name}</li>;
* - If isPacked is true, the component will return nothing, null. Otherwise,
it will return JSX to render.
* - In practice, returning null from a component isn’t common because it might surprise a developer
trying to render it. More often, you would conditionally include or exclude the component
in the parent component’s JSX.
*/
/******* Conditionally including JSX ********/
/**
*
if (isPacked) {
return <li className="item">{name} ✔</li>;
}
return <li className="item">{name}</li>;
* - While the duplication on the code above isn’t harmful, it could make your code harder to maintain.
What if you want to change the className? You’d have to do it in two
places in your code! In such a situation, you could conditionally include a
little JSX to make your code more DRY.
*/
/**
* Conditional (ternary) operator (? :)
* - Instead of duplicating, we can write this:
return (
<li className="item">
{isPacked ? name + ' ✔' : name}
</li>
);
* - You can read it as “if isPacked is true, then (?) render name + ' ✔', otherwise (:) render name”.
*/
/**
* Are these two examples fully equivalent?
*
* - If you’re coming from an object-oriented programming background, you might assume that the
* two examples above are subtly different because one of them may create two different “instances”
* of <li>. But JSX elements aren’t “instances” because they don’t hold any internal state and
* aren’t real DOM nodes. They’re lightweight descriptions, like blueprints. So these two examples,
* in fact, are completely equivalent.
*/
/**
* - If your components get messy with too much nested conditional markup, consider extracting
* child components to clean things up.
*/
/**
* Logical AND operator (&&)
* - With &&, you could conditionally render the checkmark only if isPacked is true:
return (
<li className="item">
{name} {isPacked && '✔'}
</li>
);
* - You can read this as “if isPacked, then (&&) render the checkmark, otherwise, render nothing”.
* - A JavaScript && expression returns the value of its right side (in our case, the checkmark)
* if the left side (our condition) is true. But if the condition is false, the whole
* expression becomes false. React considers false as a “hole” in the JSX tree, just like
* null or undefined, and doesn’t render anything in its place.
*
* - Don’t put numbers on the left side of &&. To test the condition, JavaScript converts the
* left side to a boolean automatically. However, if the left side is 0, then the whole
* expression gets that value (0), and React will happily render 0 rather than nothing.
* (my personal preference is use !! to convert a number to boolean)
*/
/**
* Conditionally assigning JSX to a variable
* - Use an if statement to reassign a JSX expression to itemContent if isPacked is true:
*
let itemContent = name;
if (isPacked) {
itemContent = name + " ✔";
}
* - This style is the most verbose, but it’s also the most flexible.
*/
/**
* Recap
* - In React, you control branching logic with JavaScript.
* - You can return a JSX expression conditionally with an if statement.
* - You can conditionally save some JSX to a variable and then include it inside other JSX by
* using the curly braces.
* - In JSX, {cond ? <A /> : <B />} means “if cond, render <A />, otherwise <B />”.
* - In JSX, {cond && <A />} means “if cond, render <A />, otherwise nothing”.
* - The shortcuts are common, but you don’t have to use them if you prefer plain if.
*/
/**
* Challenge 1 of 3
*
function Item({ name, isPacked }) {
return (
<li className="item">
{name} {isPacked ? '✔' : '❌'}
</li>
);
}
*/
/**
* Challenge 2 of 3
*
function Item({ name, importance }) {
return (
<li className="item">
{name} {!!importance && <i> (Importance: {importance}) </i>}
</li>
);
}
*/
/**
* Challenge 3 of 3
*
function Drink({ name }) {
let plant, caffeine, age;
if (name === 'tea') {
plant = 'leaf';
caffeine = '15–70 mg/cup';
age = '4,000+ years';
} else if (name === 'coffee') {
plant = 'bean';
caffeine = '80–185 mg/cup';
age = '1,000+ years';
}
return (
<section>
<h1>{name}</h1>
<dl>
<dt>Part of plant</dt>
<dd>{plant}</dd>
<dt>Caffeine content</dt>
<dd>{caffeine}</dd>
<dt>Age</dt>
<dd>{age}</dd>
</dl>
</section>
);
}
export default function DrinkList() {
return (
<div>
<Drink name="tea" />
<Drink name="coffee" />
</div>
);
}
*/