【问题标题】:React Admin - Create record and then update itemReact Admin - 创建记录然后更新项目
【发布时间】:2021-10-22 19:00:29
【问题描述】:

我有一个反应管理应用程序。当用户单击按钮时,用户提交的项目被批准并且项目上的参数被更改。然后使用 useCreate 挂钩在第二个数据库中创建重复记录。我需要等待 useCreate 挂钩完成,然后使用创建后的 ID 更新原始记录。我不知道该怎么做。

这里是创建一个按钮的代码,让用户批准该项目,然后进行更新,创建,需要进行后续更新。

const ApproveButton = ({ record }) => {
  const [approve] = useUpdate(
    "itemsubmission",
    record.id,
    { status: ItemStatusTypes["APPROVED"] },
    record
  );

  const [create] = useCreate("items", {
    isValid: true,
    name: record.title === null ? "" : record.title
  });

  const handleClick = (record) => {
    approve();
    const result = create();
    /* This is where I need to take the results of the create function and place the 
    /* resulting ID in the first record's database. The update is easy, but I don't 
    /* know how to wait for the create to end
  };

  return (
    <FlatButton
      label={EventStatusChangeTypes["APPROVED"]}
      type={"approve"}
      onClick={handleClick}
    />
  );
};

【问题讨论】:

    标签: reactjs react-admin


    【解决方案1】:

    你可以使用 async/await,Promise。使用 async/await 给出的示例

    const handleClick = async (record) => {
        approve();
        const result = await create();
    

    【讨论】:

    • 我试过了。它不起作用。
    • useUpdate、useCreate 工作正常吗?
    • 是的。一切正常。我不认为 useCreate 会返回一个承诺
    • 那你就不用等了
    • 必须有某种方法来获取从创建中产生的数据,然后使用它。这必须是一个特点。我只是不确定该怎么做。
    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2022-01-12
    • 2016-08-08
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多