Challenge: Implement 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:
Appas the parent component andNotificationas the child component. - The parent component,
App, should pass a prop calledmessagesto the child component. Themessagesprop is an array containing messages. In the child component, you need to check the length of themessagesarray. - 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.
- To determine if there are any messages in the array, we can check its
lengthusing the length property. The syntax is -array.length. - In React, we use the operator
&&to implement the logic of anifstatement. - To construct the string correctly, replace the placeholder
amountwith the actual length of the array. - Use curly braces
{array.length}to set the array length in the string.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the Notification component should handle an empty messages array?
What would the App component look like when passing the messages prop?
Can you show an example of how the notification message would appear with 3 unread messages?
Awesome!
Completion rate improved to 2.17
Challenge: Implement Conditional Rendering – Chat Notification
Swipe to show menu
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:
Appas the parent component andNotificationas the child component. - The parent component,
App, should pass a prop calledmessagesto the child component. Themessagesprop is an array containing messages. In the child component, you need to check the length of themessagesarray. - 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.
- To determine if there are any messages in the array, we can check its
lengthusing the length property. The syntax is -array.length. - In React, we use the operator
&&to implement the logic of anifstatement. - To construct the string correctly, replace the placeholder
amountwith the actual length of the array. - Use curly braces
{array.length}to set the array length in the string.
Thanks for your feedback!