Skip to content

Contact Book app code review#2

Open
corinadev wants to merge 8 commits intoreview-1-targetfrom
review-1
Open

Contact Book app code review#2
corinadev wants to merge 8 commits intoreview-1-targetfrom
review-1

Conversation

@corinadev
Copy link

chenster added 8 commits April 7, 2025 11:04
In the official React docs, they suggest building applications by first building a static version of the app, passing data down, and only afterwards adding interactivity and passing events "up".
TODO
bc this addess a new contact, need to loop and replace
    setContacts([...contacts, updatedContact])
Must use array map & replace
Deconstructing still is confusing
margin: 0 auto;
padding: 2rem;
text-align: left;
background-color: rgb(210, 210, 210);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, when the are no contacts, the background color only spans a part of the page. You can make it span the entire window by adding a minimum height of 100% of the viewport height: min-height: 100vh;

image


const handleContactDelete = () => {
const updatedContacts = contacts.filter(
(contact) => contact.id !== editContactId
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but indeed, it would have been even better to pass the deletedContactId, just to make the code even more readable and robust.

</header>

<section>
<ContactAdd onAddNew={ (e) => handleAddNewContact(e) } />
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip: using e as the argument name here can be misleading! I initially thought e is the submit event, but it's actually the new contact; Consider renaming this to newContact or similar.


onAddNew(newContact);

e.target.reset();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this line is highlighted with red in VSCode due to Typescript not recognising that e.target has a reset method. You can fix that by telling TS this is a form element: (e.target as HTMLFormElement).reset()

return (
<div className="card flex flex-col w-96 bg-base-100 card-md shadow-sm" key={ contact.id }>
<div className="card-body">
<h1 id="cardName" className="card-title font-bold">{ contact.name }</h1>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You add the HTML id attribute to the h1 and div elements in the ContactCard component.
Since you are rendering this component once for each contact, as soon as you have more than 1 contact, you will end up with duplicated HTML ids! This is invalid HTML markup.

Do you really need the id attribute here? Consider simply removing it.
Alternatively, if you want to keep the id, consider generating it using React's useId hook, so each ContactCard will use unique ids.

@@ -0,0 +1,2 @@
@import "tailwindcss";
@plugin "daisyui";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are already importing tailwind and daisyui in App.css - consider removing them from here!


export default function ContactCard({ contact, onEdit }: ContactCardProps) {
return (
<div className="card flex flex-col w-96 bg-base-100 card-md shadow-sm" key={ contact.id }>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't really need the key attribute over here

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to remove key property, DevTool shows this

`
App.tsx?t=1744266628666:94 Each child in a list should have a unique "key" prop.

Check the render method of App. See https://react.dev/link/warning-keys for more information.
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants