Conditions are very common situations that we face while developing or designing something. For ex. What to develop and what to ignore. Usually the condition that says- “Do it” is considered TRUE whereas the condition that says- “Don’t do it” is considered FALSE.
Almost every computer language possesses this feature and so does React. In one of the previous lessons while discussing JSX and its features, we gone through an image:
const a = 2
function App() {
if(a==1) {
return Text 1
} else {
return Text 2
}
}
);
This code snippet shows that multiple returns can be handled in JSX but only if packed under some conditions. Here we seen for the first time, how an if-else statement works in React. However it’s not the only way to induce conditional rendering. We have following four ways either:
If-else statements are pretty common in many languages. A condition is tested to be either TRUE or FALSE, if it is TRUE, a certain part of code executes. Similarly if it is false, another part of code executes.
class First extends Component {
constructor() {
super();
this.state = {
isLoggedIn: false
}
}
render(){
if(this.state.isLoggedIn) {
return (
You are Successfully Logged In
);
}
else {
return (
Log In Please !!!
);
}
}
}
export default First;
Here in this code:
Since we all know that JSX allows us to assign a complete HTML element to a variable. We can use this feature to conditionally assign different elements in the same variable. Further that variable can be rendered on the screen.
class First extends Component {
constructor() {
super();
this.state = {
isLoggedIn: false
}
}
render(){
let prompt
if(this.state.isLoggedIn) {
prompt = You are Successfully Logged In
}
else {
prompt = Log In Please !!!
}
return (
{prompt}
);
}
}
export default First;
Here in this code:
div
.Ternary operators are also pretty common in many languages. This operator uses two symbols ?
and :
in the following way-
Important is to keep the entire statement as one single statement.
class First extends Component {
constructor() {
super();
this.state = {
isLoggedIn: false
}
}
render(){
return this.state.isLoggedIn ? (
You are Successfully Logged In
) : (
Log In Please !!!
)
}
}
export default First;
Here in this code:
div
is displayed and similarly when it is true, the first div
is displayed.We can also use the ternary operators to render a certain part or block of a statement.
Look at the code below:
class First extends Component {
render() {
const isLoggedIn = false;
return (
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
);
}
}
export default First;
Short circuit operators are also quite common but in the languages influenced by JAVA. JavaScript also uses short circuit operators. In these operators, the condition to the left of &&
is tested and when found to be TRUE, the statement to the right of it executes. Interestingly when the condition is found to be false, nothing is executed.
class First extends Component {
constructor() {
super();
this.state = {
isLoggedIn: false
}
}
render(){
return this.state.isLoggedIn && You are Successfully Logged In
}
}
export default First;
Here in this code:
div
is displayed.