DEV Community

Cover image for Creating a CRUD app in React with Hooks

Creating a CRUD app in React with Hooks

sanderdebr on March 08, 2020

In this tutorial we will build a create, read, update and delete web application with React using React Hooks. Hooks let us use state and other fea...
Collapse
 
austejakazlauskyte profile image
Austeja Kazlauskyte • Edited

Hey again!
I have a question.
Let's say my User object not only has fields like Id, Name, and Username, but it also has an Address. Inside the Address field, it has City and ZIP.
In other words, the User has another object inside it.
If I would like to add this User object using a form, how could I reach those second-level properties, like City, in order to update the name?

Thank you for any tips!
Austeja

Collapse
 
sanderdebr profile image
sanderdebr • Edited

Hi no problem!

I would create an exception for your ZIP and City fields. When the name of the fields is one of those, the function will update the user object.

const handleChange = (e) => {
    const { name, value } = e.target;

    if (name === "zip" || name === "city") {
      setUser({ ...user, address: { [name]: value } });
    } else {
      setUser({ ...user, [name]: value });
    }
  };
Enter fullscreen mode Exit fullscreen mode

It is also possible to build a function that searches all the object keys in the user object, and updated the one you want if it finds it but this is a bit more work.

The three dots indicate the object spread, this automatically will create a copy of the object and update the values you want. Another way is to use const draftUser = Object.assign({}, user); to create a copy of the user object. Then you can object this draftUser object and update the state with it :-)

Collapse
 
austejakazlauskyte profile image
Austeja Kazlauskyte

thank you, amazing!

Collapse
 
austejakazlauskyte profile image
Austeja Kazlauskyte

I ran into a problem with the EditUserForm method.
The Edit button is not working;/
I have a feeling it has to do this method:
const handleChange = e => {
const {name: name, value} = e.target;
setUser({...user, [name]: value});
}

I've got an underline saying: Argument type {} is not assignable to parameter type ((prevState: EditUserForm.props.currentUser) => EditUserForm.props.currentUser) | EditUserForm.props.currentUser

Any ideas what could be wrong?

Collapse
 
sanderdebr profile image
sanderdebr

Hi, for me it still works using your piece of code. Please checkout my repo if you can find any differences:
github.com/sanderdebr/react-crud-h...

If not you could show me a bit more of your code

Collapse
 
austejakazlauskyte profile image
Austeja Kazlauskyte

A million thanks for your reply.

I solved the issue by myself. Apparently, I wrote the functions but did not insert the onClick method on the button, to trigger everything.
A beginner mistake.

Thank you for a great tutorial, it made it very easy for me to understand how to do CRUD with React.

And to be honest - through you, I found Skeleton and immediately fell in love with this framework. I am definitely going to use it in the future when I need smth fast to bootstrap my small coding projects.

Thread Thread
 
austejakazlauskyte profile image
Austeja Kazlauskyte

Hey, another question if you may - how would you fetch data from an API?
Would you just replace the data.js file with a function that receives data using Hooks, or would you recommend something else? Thank you!

Thread Thread
 
sanderdebr profile image
sanderdebr

Hi, thanks for the suggestion I've just added a Bonus section where I explain how to fetch user data from an API. I think it is a very helpful addition to this tutorial.

The way I did it might be a bit hard to understand, there are easier ways to fetch data in React but in this way you'll also learn about custom hooks :)

Also, I've added a little update for adding multiple users with the same data.

Thread Thread
 
austejakazlauskyte profile image
Austeja Kazlauskyte

That is amazing, thanks! ⭐️💜👍🏻

Collapse
 
nguyentung59 profile image
Tung Nguyen • Edited

In ./tables/UserTable.jsx You add onClick={() => props.editUser(id, user)} into button Edit

Collapse
 
danielpdev profile image
danielpdev • Edited

Great post!
It has a little issue.
It does not work well when you hit twice add user for the same data.

Collapse
 
sanderdebr profile image
sanderdebr • Edited

Thanks, I've added a fix for this!

Collapse
 
vinh2203 profile image
Vinh2203

you can modify the handleSubmit function in AddUserForm.jsx by adding this line
setUser(initUser);

Collapse
 
claudewill1 profile image
Claude Will • Edited

One issue I ran into with this is that the deleteUser method deletes all users from the table when I just click delete on one of them.

Any idea how I can correct this? I originally found this tutorial on another website then did a search when I ran into the issue and came here. I am some what "new" to React and can't figure out why it's deleting all with clicking the delete button on a single item.

Thank you

Collapse
 
claudewill1 profile image
Claude Will

Found the issue. Heh I feel like an idiot, I always seem to have something really small that I'm missing. It's working fine now. Thanks for the article.

Collapse
 
termyn9 profile image
termyn9 • Edited

Hey! U did a perfect course about CRUD- app, but i do not undrestand something... Could you show how to realize POST, ADD, DELETE methods of API? I really need your help, I can't cope on my own.

Collapse
 
vnivy profile image
vnivy

Hey!
I have a question about how to add 5 to 6fields if tried adding those but its not working for me it would be nice if u help me :)
thanks in advance.

Collapse
 
ardsb profile image
Mohammed Arkam

why I'm not unable to input any values in the input name and username fields to add a data.