Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Rendering Content Conditionally | Introduction to React Fundamentals
course content

Course Content

Mastering React

Challenge: Rendering Content ConditionallyChallenge: Rendering Content Conditionally

Task: Creating Chat Notification

Let's devise a mechanism to display a notification only when a user has unread messages. We will verify whether the user has any unread messages. If this condition is true, we will show a notification indicating the number of messages. However, we won't display anything if the user has no messages.

The task is:

  • Create two components: App as the parent component and Notification as the child component.
  • The parent component, App, should pass a prop called messages to the child component. The messages prop is an array containing messages. In the child component, you need to check the length of the messages array.
  • If there are any messages in the array, display a string that says: You have <amount> of unread messages. The <amount> should be replaced with the actual count of unread messages.
Hint
1. To determine if there are any messages in the array, we can check its length using the length property. The syntax is - array.length.
2. In React, we use the operator && to implement the logic of an if statement.
3. To construct the string correctly, replace the placeholder amount with the actual length of the array.
4. Use curly braces {array.length} to set the array length in the string.

Task: Creating a Bank Alert

Let's work with a bank account. We aim to develop a logic that informs the user whether they have sufficient funds on their card to cover a given price. Depending on the outcome, we will display different messages to the user.

The task is:

  • Create two components - App, parent, and Message, child.
  • The parent component (App) transfers the props moneyAvailable, price, and name.
    1. moneyAvailable - represents the amount of money the user has.
    2. price - denotes the amount of money the user needs to pay.
    3. name - refers to the user's name.
  • If the user has enough money to pay, we show the message: Success! The remaining amount of money for <user_name> is: <calculated_amount_of_money_left>. Thank you!.
  • If the user has not enough money to pay, we show the message: Failure! The bank account balance for <user_name> is: <moneyAvailable>. Unfortunately, you need to pay: <price>.
Hint
1. In React, we use the ternary operator condition ? ... : ... to implement the logic of an if...else statement.
2. To construct the string correctly,
  •     - replace the placeholder user_name with the actual user name: name prop.
  •     - replace the placeholder calculated_amount_of_money_left with the calculated amount of money left - moneyAvailable - price.
  •     - replace the placeholder moneyAvailable with the actual available money - moneyAvailable prop.
  •     - replace the placeholder price with the actual price - price prop.
  • 3. Use curly braces {...} to assign a value that is not a string type.

    Everything was clear?

    Section 1. Chapter 12
    course content

    Course Content

    Mastering React

    Challenge: Rendering Content ConditionallyChallenge: Rendering Content Conditionally

    Task: Creating Chat Notification

    Let's devise a mechanism to display a notification only when a user has unread messages. We will verify whether the user has any unread messages. If this condition is true, we will show a notification indicating the number of messages. However, we won't display anything if the user has no messages.

    The task is:

    • Create two components: App as the parent component and Notification as the child component.
    • The parent component, App, should pass a prop called messages to the child component. The messages prop is an array containing messages. In the child component, you need to check the length of the messages array.
    • If there are any messages in the array, display a string that says: You have <amount> of unread messages. The <amount> should be replaced with the actual count of unread messages.
    Hint
    1. To determine if there are any messages in the array, we can check its length using the length property. The syntax is - array.length.
    2. In React, we use the operator && to implement the logic of an if statement.
    3. To construct the string correctly, replace the placeholder amount with the actual length of the array.
    4. Use curly braces {array.length} to set the array length in the string.

    Task: Creating a Bank Alert

    Let's work with a bank account. We aim to develop a logic that informs the user whether they have sufficient funds on their card to cover a given price. Depending on the outcome, we will display different messages to the user.

    The task is:

    • Create two components - App, parent, and Message, child.
    • The parent component (App) transfers the props moneyAvailable, price, and name.
      1. moneyAvailable - represents the amount of money the user has.
      2. price - denotes the amount of money the user needs to pay.
      3. name - refers to the user's name.
    • If the user has enough money to pay, we show the message: Success! The remaining amount of money for <user_name> is: <calculated_amount_of_money_left>. Thank you!.
    • If the user has not enough money to pay, we show the message: Failure! The bank account balance for <user_name> is: <moneyAvailable>. Unfortunately, you need to pay: <price>.
    Hint
    1. In React, we use the ternary operator condition ? ... : ... to implement the logic of an if...else statement.
    2. To construct the string correctly,
  •     - replace the placeholder user_name with the actual user name: name prop.
  •     - replace the placeholder calculated_amount_of_money_left with the calculated amount of money left - moneyAvailable - price.
  •     - replace the placeholder moneyAvailable with the actual available money - moneyAvailable prop.
  •     - replace the placeholder price with the actual price - price prop.
  • 3. Use curly braces {...} to assign a value that is not a string type.

    Everything was clear?

    Section 1. Chapter 12
    some-alt