react-admin:无需HTTP请求即可更改存储中的资源数据

时间:2018-06-29 10:17:57

标签: react-admin

我需要修改存储中的资源数据:

State->admin->resources->Orders->data

但不调用http请求,如文档示例所示:

// in src/comment/commentActions.js
import { UPDATE } from 'react-admin';
export const COMMENT_APPROVE = 'COMMENT_APPROVE';
export const commentApprove = (id, data, basePath) => ({
    type: COMMENT_APPROVE,
    payload: { id, data: { ...data, is_approved: true } },
    meta: { resource: 'comments', fetch: UPDATE },
});

有可能吗?

1 个答案:

答案 0 :(得分:2)

是的,有可能。但是,由于我们通常会处理乐观的行为和其他技巧,所以这并不容易。

如果您需要其他类型的操作类型(例如sagas或自定义减速器):

// in src/comment/commentActions.js
import { UPDATE, FETCH_END } from 'react-admin';

export const COMMENT_APPROVE = 'COMMENT_APPROVE';

export const commentApprove = (id, data, basePath) => ({
    type: COMMENT_APPROVE,
    payload: { id, data: { ...data, is_approved: true } },
    meta: { resource: 'comments', fetchResponse: UPDATE, fetchStatus: FETCH_END },
});

如果不这样做:

// in src/comment/commentActions.js
import { CRUD_UPDATE_OPTIMISTIC } from 'react-admin';

export const commentApprove = (id, data, basePath) => ({
    type: CRUD_UPDATE_OPTIMISTIC,
    payload: { id, data: { ...data, is_approved: true } },
    meta: { resource: 'comments' },
});