-
Notifications
You must be signed in to change notification settings - Fork 111
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
Shelby Faulconer, Sea Turtles, C17 #99
base: main
Are you sure you want to change the base?
Conversation
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.
First React project down! 🎉 I've left some feedback & suggestions, please take a look when you have time & reach out if there's anything I can clarify 😄
const countLikes = () => { | ||
let counter = 0; | ||
for (let entry of entries) { | ||
if (entry.liked) { | ||
counter += 1; | ||
}; | ||
}; | ||
return counter; | ||
}; |
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.
Great use of the existing data to calculate the total likes! Another option would be to use the array function reduce
:
return messageData.reduce((totalLikes, currentMessage) => {
// If currentMessage.liked is true add 1 to totalLikes, else add 0
return (totalLikes += currentMessage.liked ? 1 : 0);
}, 0); // The 0 here sets the initial value of totalLikes to 0
return { | ||
id: entry.id, | ||
sender: entry.sender, | ||
body: entry.body, | ||
timeStamp: entry.timeStamp, | ||
liked: !entry.liked | ||
}; |
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 managing of data & making sure we make a new object when we need to alter a message in our list!
A best practice for copying over an object's values is to use the spread operator, then overwrite any key/value pairs we want to change:
// Use the spread operator to copy all of entry's values to a new object,
// then overwrite the attribute liked with the opposite of entry's liked value
return {
...entry,
liked: !entry.liked
}
const heartIcon = (props.liked) ? '❤️' : '🤍'; | ||
const messageAlign = (props.sender === 'Vladimir') ? 'remote' : 'local'; |
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.
Ternary operators! 🎉
<button className="like">🤍</button> | ||
<p>{props.body}</p> | ||
<p className="entry-time"><TimeStamp time={props.timeStamp}/></p> | ||
<button className="like" onClick={() => {props.setLiked(props.id)}}>{heartIcon}</button> |
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.
I like this pattern of sending just the id
to props.setLiked
since it keeps all the state management and message object creation confined to App
.
id: PropTypes.number.isRequired, | ||
sender: PropTypes.string.isRequired, | ||
body: PropTypes.string.isRequired, | ||
timeStamp: PropTypes.string.isRequired, | ||
liked: PropTypes.bool.isRequired, |
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 Prop-Types
and flagging those necessary for render with isRequired!
. To help our future selves (or other folks we're working with) easily see what is allowed to be passed when creating an instance of a component, I suggest including all props that can be passed in, including functions such as setLiked
.
); | ||
}; | ||
|
||
ChatLog.propTypes = { | ||
entries: PropTypes.arrayOf(PropTypes.object).isRequired, |
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.
We can be even more specific with our PropTypes for validation. If we know that we need an array, and that array's objects need to hold certain data, we can check the objects as well! Inside arrayOf
we can pass PropTypes.shape()
with information about the object's shape. A modified example from stack overflow is below:
list: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
customTitle: PropTypes.string.isRequired,
btnStyle:PropTypes.object,
})
).isRequired,
Source: https://stackoverflow.com/questions/59038307/reactjs-proptypes-validation-for-array-of-objects
Similar to some of the feedback for ChatEntry's PropTypes, it would be good to list the function ChatLog accepts here as well.
No description provided.