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 and setState, since in this context, the values don't come from the state anymore.
Change value to defaultValue. We want to set the starting value, but we do not want to track further state changes
Add a new onSubmit function that will use updateContact
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
Contact Component
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