3

I have a simple app for CRUD operations, now I can delete, edit, and display data, but I want to add new items to a list of users fetched from API(JSON server fake API) using react and redux.

Now when I click add user and save the data only id is sent but other data are not sent.

enter image description here

here is a live demo: Redux live demo with source code sandbox .

Here is my form, AddUserForm.js

import React, { useState } from 'react'
import {useDispatch} from 'react-redux'
import { addNewUser  } from '../redux/acitons/users/Users';

function AddUserForm({users}) {
    const dispatch = useDispatch();
    const [newUserName, setNewUserName] = useState('');

   const handleSubmit = (e) =>{
    e.preventDefault();

    const usersList = users;
    const newUserId = usersList[usersList.length - 1].id + 1;

    console.log("your name", newUserName);
    console.log("your id", newUserId)

    dispatch(addNewUser({
      ...users,
      id: newUserId,
      name: newUserName,
    }));

   }

  const handleCancel = () => {};
  return (
    <tr>
      <td>{newUserName.id}</td>
      <td>
        <input
          value={newUserName}
          name="name"
          onChange={e => setNewUserName(e.target.value)}
        />
      </td>
      <td>
        <button type="button" className="btn outline" onClick={handleCancel}>
          <i className="material-icons">Cancel</i>
        </button>
        <button
          type="button"
          className="btn btn-success btn-link"
          onClick={handleSubmit}
        >
          <i className="material-icons">save</i>
        </button>
      </td>
    </tr>
  );
}

export default AddUserForm

and I call like this in parent components with the on click button.

{adding && <>
            <AddUserForm addNewUser={addNewUser} users={userData.users} />
   </>}

Here is a button in the parent components.

<button
          type="button"
          className="btn btn-success btn-link btn-add"
          onClick={() => setAdding(true)}
        >
    <i className="material-icons">Add user</i>
</button>

What is wrong with my code? any help or suggestions will be appreciated.

1

1 Answer 1

2
+100

In the debugging tools screenshot you are showing us the response body, not the request body.

users is an array, why this

dispatch(addNewUser({
  ...users,
  id: newUserId,
  name: newUserName,
}));

intead of this?

dispatch(addNewUser({
  id: newUserId,
  name: newUserName,
}));

The root cause of the problem is in src/redux/acitons/users/Users

export const addNewUser = data => {

  console.log("adding mother a new user", data);

  return dispatch => {
    axios.post("http://localhost:3000/users")

Just change last line in

    axios.post("http://localhost:3000/users", data)

I can see data in the console.log not in the POST.

This gives error with empty usersList.

const newUserId = usersList[usersList.length - 1].id + 1;

Hope this helps.

Sign up to request clarification or add additional context in comments.

10 Comments

I changed that but still the same, u can clone the repo and test it on your side
The app opens with "Request failed with status code 404" maybe a back-end is missing?
@TheDeadMan anyway in the repos I cloned data is still missing in the POST request
Daniel sorry i didnt tell you , you need first to run json-server --watch users.json and then on another terminal run yarn start and u should see the data, am using json server fake api
@TheDeadMan I simply added data to the POST request and it worked... please check my edited answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.