-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C22 -Phoenix -- Beenish Ali #35
base: main
Are you sure you want to change the base?
Changes from 1 commit
3101aa6
f40e007
d3c6871
3a7c37c
fa32bbf
f85d7d6
e7ba0f3
c317629
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,12 @@ import Data from './data/messages.json'; | |
import ChatLog from './components/ChatLog'; | ||
import { useState } from 'react'; | ||
|
||
|
||
function App () { | ||
const [messageData, setMessageData] = useState(Data); | ||
|
||
const senders = [...new Set(Data.map(message => message.sender))]; | ||
|
||
const handleLiked = (id) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Since our state is defined here, we also need to define our mutating function here so that it can "see" the setter function. All we need to receive is the |
||
setMessageData(messageData => messageData.map(message => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of the callback setter style. In this application, it doesn't really matter whether we use the callback style or the value style, but it's good practice to get in the habit of using the callback style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of map here to both handle making a new list so that React sees the message data has changed, and make new data for the clicked message with its like status toggled. |
||
if (message.id === id) { | ||
|
@@ -32,7 +35,7 @@ function App () { | |
<> | ||
<div id="App"> | ||
<header> | ||
<h1>ChatLog</h1> | ||
<h1>Chat between {senders[0]} and {senders[1]}</h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the conversation in the project, there are exactly two participants and both have sent messages, which is the only reason we can find them. What would happen for other conversation situations?
Some of these cases might not really be workable given the limited data we're working with, but it's worth thinking about what this could look like for a more complete application. |
||
<h2>{totalLiked}❤️s</h2> | ||
</header> | ||
<main> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of
map
to extract the sender information, then uniquing that with aSet
to get the participants in the conversation. We can confirm with the docs that iterating over theSet
contents will occur in insertion order, meaning the resulting list of senders, however many there are, will be in the order they participated in the conversation.