2

我有一个用于编辑子实体的表单hasMany。在这个表单中有一个ReferenceManyField组件。该组件列出了项目以及我创建的操作按钮,让我们说“批准”事情。

<Edit>
  <SimpleForm>
    <ReferenceManyField reference="Expenses" target="expenseReportId" label="Expenses">
      <Datagrid>
        <UnassignButton />
        <TextField source="name" />
      </Datagrid>
    </ReferenceManyField>
  </SimpleForm>
</Edit>

UnassignButton 执行常规操作:

const action = (id, data, basePath) => ({
  type: 'EXPENSE_UNASSIGN',
  payload: { id: id, data: { ...data, expenseReportId: null } },
  meta: { resource: basePath, fetch: UPDATE, cancelPrevious: false },
})

执行此操作时,如何强制刷新 ReferenceManyField 或整个 Edit 表单?最好的做法是什么?

谢谢!

4

2 回答 2

1

现在你可以在 react-admin 中使用 useRefresh 钩子了

常量刷新 = useRefresh();

当您想更新列表时只需使用刷新

import * as React from "react";
import { useMutation, Button } from 'react-admin';
import {  useNotify, useRedirect, useRefresh } from 'react-admin';

export const MyButton = ({ record }) => {

    const notify = useNotify();
    const redirect = useRedirect();
    const refresh = useRefresh();
 
    const [createPost, { loading }] = useMutation({
        type: 'create',
        resource: 'posts',
        payload: { data:
                    {
                      userId: "xxxxx",
                      body: "something"
                      isConfirmed: false
                    }
                 }

    },
    {
        onSuccess: ({ data }) => {
            redirect('/posts');
            refresh();
            notify('post created');
        },
        onFailure: (error) => notify(`error: ${error.message}`, 'warning'),
    });

    return (
        <Button label="CREATE" onClick={ createPost } } disabled={loading} >
            Create Post
        </Button>
    )
};

于 2020-08-04T15:27:56.780 回答
1

我会派出

import { refreshView } from "react-admin";

改变状态后的动作。

于 2018-03-06T13:54:23.917 回答