-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserGreeting.js
51 lines (42 loc) · 1.15 KB
/
UserGreeting.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
import React, { Component } from "react";
class UserGreeting extends Component {
constructor(props) {
super(props);
this.state = {
isLoggedIn: true,
};
}
render() {
// 1 way
// if (this.state.isLoggedIn) {
// return <div>Welcome Saish</div>;
// } else {
// return <div>Welcome Guest</div>;
// }
// 2 way
// let message;
// if (this.state.isLoggedIn) {
// message = <div>Welcome Saish</div>;
// } else {
// message = <div>Welcome Guest</div>;
// }
// return message;
// 3 way
// return this.state.isLoggedIn ? (
// <div>Welcome Saish</div>
// ) : (
// <div>Welcome Guest</div>
// );
// 4 way
return this.state.isLoggedIn && "Welcome Saish";
// if isLoggedIn then it print message otherwise it will be blank.
}
}
export default UserGreeting;
// There are 4 way for conditional render
// 1) if/else statement
// 2) element variable
// 3) Ternary conditional operator
// 4) Short circuit operator
/* if/else statement not work in JSX. JSX is just syntactic sugar for function calls and object construction. */
// In most cases use 3 or 4 way