Skip to content Skip to sidebar Skip to footer

How To Insert Data With Post Using React Redux?

I am learning react-redux, so I am creating a CRUD app for users using JSON placeholder API, now I am able to display data, delete and edit, but I have a problem with adding data.

Solution 1:

Looking at your code - it seems like your app is breaking since you are not passing any user-payload object to theaddUser dispatch call(on User.js line 64).

Here's a possible way to solve this:

Passing a new-user payload object with an id 1 higher than the previous user on the list) and then dispatch an editUser for that new id.

constaddNewUser = () => {
    const usersList = userData.users;
    const newUserId = usersList[usersList.length - 1].id + 1;
    addUser({
      id: newUserId
    });
    editUser(newUserId);
  };

This is a very simplified solution (for example - a better approach would be to give a unique id to each user and index the list on the client not based on the server ids) - but it should give you an idea of how to take this forward.

Solution 2:

Well first of all, to temporary handle compile errors put question marks on user?.editing and other places where you use user.id or other parameters. Other thing, I can see that you are using id for editing and deleting so you should also set id property in your userForm

Post a Comment for "How To Insert Data With Post Using React Redux?"