【问题标题】:Post Method with React and redux使用 React 和 redux 的 Post 方法
【发布时间】:2020-06-01 17:56:34
【问题描述】:

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

现在,当我点击添加用户并保存数据时,仅发送 id,但不发送其他数据。

这是一个现场演示:Redux live demo with source code sandbox

这是我的表单,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

我在父组件中使用点击按钮这样调用。

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

这是父组件中的一个按钮。

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

我的代码有什么问题?任何帮助或建议将不胜感激。

【问题讨论】:

标签: javascript html reactjs post react-redux


【解决方案1】:

在调试工具屏幕截图中,您向我们展示的是响应正文,而不是请求正文。

users 是一个数组,为什么这样

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

这个?

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

问题的根本原因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")

只需更改最后一行

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

我可以在console.log 中看到data 而不是在 POST 中。

这会产生空usersList 的错误。

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

希望这会有所帮助。

【讨论】:

  • 我改变了,但还是一样,你可以克隆 repo 并在你身边测试它
  • 应用打开时显示“请求失败,状态码为 404”,可能缺少后端?
  • @TheDeadMan 在我克隆的仓库中 data 在 POST 请求中仍然缺失
  • Daniel 对不起,我没有告诉你,你需要先运行json-server --watch users.json,然后在另一个终端上运行yarn start,你应该会看到数据,我正在使用 json server fake api
  • @TheDeadMan 我只是在 POST 请求中添加了data,它起作用了...请检查我编辑的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2021-06-23
  • 2020-06-04
  • 2018-06-06
相关资源
最近更新 更多