React Part II
Let's take a moment to think what this component should do:
it should:
display the name and email of the contact
allow the user to press an "edit" button, which opens an edit form
in edit mode, when the user click "cancel", the edit form should close, and the content of it should reset
in edit mode, when the user click "ok", a request should be sent to update the user
if the user presses "delete", a request should be sent to delete the user
We can define two types of things here:
name
,email
are properties held in the parent component, and not decided by ourContact
component. They will be passed as propsupdateContact
,deleteContact
are methods in the parent component, and will be passed as props also.updateContact
anddeleteContact
both need to pass anid
property, which should be passed as a prop too.editMode
is an internal state of the component. The parent component isn't concerned if theContact
component is opened or closed.
We have state
, which means, we are going to use a class component.
Create a new file, Contact.js
Here's what we can write, based on what we know:
We already have renderViewMode
. It's what we were using previously in the App
loop.
Let's copy paste it out of there, and modify it slightly:
We still have to handle renderEditMode
. Wait! It's suspiciously close to the "create" form! Let's copy-paste it from there. We need to operate a few changes though:
Remove references to
state
andsetState
, since in this context, the values don't come from the state anymore.Change
value
todefaultValue
. We want to set the starting value, but we do not want to track further state changesAdd a new
onSubmit
function that will useupdateContact
Add a
name
property to each input, so we can find them back
Notice how the form's onReset
toggles the edit mode too, so when a user clicks "cancel", the form switches back the view mode.
We just need something to switch from edit mode to view mode. Add a new button:
Stick this somewhere next to the delete button.
The whole component will look like
Almost done, now, to see it, go to App.js
, and do a little
at the top. Then, use it in the loop:
Replace where
by
Now you can:
insert a new contact in the database
delete a contact
update a contact
list all contacts
Congrats! You have a fully functional CRUD. This is not bad at all, ...for a start
Last updated
Was this helpful?