简体   繁体   English

使用 React 和 redux 的发布方法

[英]Post Method with React and redux

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.我有一个用于 CRUD 操作的简单应用程序,现在我可以删除、编辑和显示数据,但我想使用 react 和 redux 将新项目添加到从 API(JSON 服务器假 API)获取的用户列表中。

Now when I click add user and save the data only id is sent but other data are not sent.现在,当我单击添加用户并保存数据时,仅发送 id,但不发送其他数据。

在此处输入图像描述

here is a live demo: Redux live demo with source code sandbox .这是一个现场演示: Redux 现场演示和源代码沙箱

Here is my form, AddUserForm.js这是我的表单,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.任何帮助或建议将不胜感激。

In the debugging tools screenshot you are showing us the response body, not the request body.在调试工具屏幕截图中,您向我们展示了响应正文,而不是请求正文。

users is an array, why this users是一个数组,为什么这样

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问题的根本原因在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.我可以在console.log中看到data ,而不是在 POST 中。

This gives error with empty usersList .这给出了空usersList的错误。

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

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM