Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Conditional Rendering - Chat Notification | Introduction to React Fundamentals
Mastering React

Challenge: Conditional Rendering - Chat NotificationChallenge: Conditional Rendering - Chat Notification

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.
  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.

Tudo estava claro?

Seção 1. Capítulo 12
course content

Conteúdo do Curso

Mastering React

Challenge: Conditional Rendering - Chat NotificationChallenge: Conditional Rendering - Chat Notification

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.
  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.

Tudo estava claro?

Seção 1. Capítulo 12
some-alt