API调用后再次触发useEffect

时间:2020-07-02 10:22:14

标签: reactjs react-hooks

我有以下useEffect挂钩,它在页面的初始加载时运行

    app.Map("/h1/h2", handle2);
    app.Map("/h1/h3", handle3);
    app.Map("/h1", handle1);

,然后使用以下功能将新数据添加到api。在表单提交时调用此功能。

useEffect(() => {
  const getLeads = () => axios.get("/api/leads");

  const setLeadState = async () => {
    try {
      const res = await getLeads();
      res.data.map(lead => setLeads(prevState => [...prevState, lead]));
    } catch (err) {
      console.error(err);
    }
  };
  setLeadState();
}, []);

在执行addLead函数之后,如何再次调用useEffect挂钩?我尝试过这样的事情

const addLead = async (firstName, lastName, email) => {
  try {
    const body = {
      firstName,
      lastName,
      email
    };
    const res = await axios.post("api/leads", body);
  } catch (err) {
    console.error(err);
  }
};

然后在addLead函数中更改“测试”,但这会导致无限循环,页面根本无法呈现。

1 个答案:

答案 0 :(得分:1)

将功能移至获取/设置效果之外的数据的位置,然后在System.Web.HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x800703E3. ---> System.Runtime.InteropServices.COMException (0x800703E3): The I/O operation has been aborted because of either a thread exit or an application request. (Exception from HRESULT: 0x800703E3) at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size) at System.Web.Hosting.IIS7WorkerRequest.ReadEntityBody(Byte[] buffer, Int32 size) at System.Web.HttpRequest.GetEntireRawContent() at System.Web.HttpRequest.GetMultipartContent() at System.Web.HttpRequest.FillInFilesCollection() at System.Web.HttpRequest.EnsureFiles() at System.Web.HttpRequest.get_Files() at Lic.Web.Controllers.FileUploadController.<UploadFileAsync>d__6.MoveNext() in C:\projects\emclient\license-manager\Lic.Web\Controllers\FileUploadController.cs:line 93 之后调用该功能

addLead