-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSXConditionals.js
129 lines (102 loc) · 2.66 KB
/
JSXConditionals.js
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
// Esta es una forma común de expresar condicionales en JSX.
import React from 'react';
import ReactDOM from 'react-dom';
let message;
if (user.age >= drinkingAge) {
message = (
<h1>
Hey, check out this alcoholic beverage!
</h1>
);
} else {
message = (
<h1>
Hey, check out these earrings I got at Claire's!
</h1>
);
}
ReactDOM.render(
message,
document.getElementById('app')
);
// Ohter Example
import React from 'react';
import ReactDOM from 'react-dom';
function coinToss() {
// This function will randomly return either 'heads' or 'tails'.
return Math.random() < 0.5 ? 'heads' : 'tails';
}
const pics = {
kitty: 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-kitty.jpg',
doggy: 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-puppy.jpeg'
};
let img;
// if/else statement begins here:
if (coinToss() === "heads"){
img = (
<img src={pics.kitty} />
);
}else{
img = (
<img src={pics.doggy} />
);
}
ReactDOM.render(
img,
document.getElementById('app')
);
// el operador ternario
// El operador ternario funciona de la misma manera en React
//que en JavaScript normal. Sin embargo, aparece en React sorprendentemente a menudo
const headline = (
<h1>
{ age >= drinkingAge ? 'Buy Drink' : 'Do Teen Stuff' }
</h1>
);
// Example
import React from 'react';
import ReactDOM from 'react-dom';
function coinToss () {
// Randomly return either 'heads' or 'tails'.
return Math.random() < 0.5 ? 'heads' : 'tails';
}
const pics = {
kitty: 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-kitty.jpg',
doggy: 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-puppy.jpeg'
};
const img = <img src={pics[coinToss() === 'heads' ? 'kitty' : 'doggy']} />;
ReactDOM.render(
img,
document.getElementById('app')
);
// Conditionals: &&
// Al igual que el operador ternario, &&no es específico de React, pero aparece en React sorprendentemente a menudo
const tasty = (
<ul>
<li>Applesauce</li>
{ !baby && <li>Pizza</li> }
{ age > 15 && <li>Brussels Sprouts</li> }
{ age > 20 && <li>Oysters</li> }
{ age > 25 && <li>Grappa</li> }
</ul>
);
// Example
import React from 'react';
import ReactDOM from 'react-dom';
// judgmental will be true half the time.
const judgmental = Math.random() < 0.5;
const favoriteFoods = (
<div>
<h1>My Favorite Foods</h1>
<ul>
<li>Sushi Burrito</li>
<li>Rhubarb Pie</li>
{ !judgmental && <li>Nacho Cheez Straight Out The Jar</li> }
<li>Broiled Grapefruit</li>
</ul>
</div>
);
ReactDOM.render(
favoriteFoods,
document.getElementById('app')
);