Lesson 2 of 39
Lesson Progress: 0%
function StatusMessage() {
const isLoading = false;
return (
<div>
{isLoading ? <p>Loading...</p> : <p>Data loaded.</p>}
</div>
);
}
render(<StatusMessage/>);Instructions
▼ ← Click the triangle to hide or reveal instructions.StatusMessage is a function component, which means it is a function that returns JSX to display on the screen.isLoading ? <p>Loading...</p> : <p>Data loaded.</p> means "if isLoading is true, show Loading..., otherwise show Data loaded." are needed because they let us place JavaScript logic inside the JSX.function CheckIfLoggedIn() {
const isLoggedIn = true;
return (
<div>
<h2>Example 1</h2>
{isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}
</div>
);
}
render(<CheckIfLoggedIn/>);Instructions
▼ ← Click the triangle to hide or reveal instructions.CheckIfLoggedIn is a function declaration component, which means it is a named function that returns JSX.isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p> means if isLoggedIn is true, show Welcome back!, otherwise show Please log in. are needed because they let JavaScript code work inside the JSX.function CheckForNewMessages() {
const hasMessages = false;
return (
<div>
<h2>Example 2</h2>
{hasMessages ? <p>You have new messages.</p> : <p>No new messages.</p>}
</div>
);
}
render(<CheckForNewMessages/>);Instructions
▼ ← Click the triangle to hide or reveal instructions.CheckForNewMessages is a function declaration component, which means it is a named function that returns JSX to show on the page.? means "if true, show this part" and the : means "otherwise, show this other part." are needed because they let us place JavaScript logic inside the JSX.const CheckIfLoggedIn = function () {
const isLoggedIn = true;
return (
<div>
<h2>Example 1</h2>
{isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}
</div>
);
};
render(<CheckIfLoggedIn/>);Instructions
▼ ← Click the triangle to hide or reveal instructions.CheckIfLoggedIn is a function expression component, which means a function is being stored inside a variable and used as a component.? means "show this if the value is true" and the : means "otherwise show this other part." are needed because they let JavaScript logic work inside the JSX.Instructions
▼ ← Click the triangle to hide or reveal instructions.CheckForNewMessages variable.? means "show this part if the value is true" and the : means "otherwise show the other part." are needed because they let JavaScript logic go inside the JSX.Instructions
▼ ← Click the triangle to hide or reveal instructions.=> arrow, instead of writing the word function.isLoggedIn ? checks if isLoggedIn is true, and if it is, the first message will be shown. are needed because they let JavaScript logic go inside the JSX.Instructions
▼ ← Click the triangle to hide or reveal instructions.=> arrow, instead of using the word function.hasMessages ? asks, "Is hasMessages true?" If it is, the first message will be shown.: means "otherwise," so it shows the second message when the value is false.ternary means this expression has three parts: the check, the true result, and the false result.Test Incomplete
const CheckForNewMessages = function () {
const hasMessages = false;
return (
<div>
<h2>Example 2</h2>
{hasMessages ? <p>You have new messages.</p> : <p>No new messages.</p>}
</div>
);
};
render(<CheckForNewMessages/>);const CheckIfLoggedIn = () => {
const isLoggedIn = true;
return (
<div>
<h2>Example 1</h2>
{isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}
</div>
);
};
render(<CheckIfLoggedIn/>);const CheckForNewMessages = () => {
const hasMessages = false;
return (
<div>
<h2>Example 2</h2>
{hasMessages ? <p>You have new messages.</p> : <p>No new messages.</p>}
</div>
);
};
render(<CheckForNewMessages/>);