2

I have a Edit component in an admin-on-rest's app which internally uses ReduxForm and the code looks like this:

<Edit {...props} >
    <CustomField />
    <TextInput source="title" />
</Edit>

I am wondering how is it possible to change title from CustomField without persisting it on the on the server?

4

1 回答 1

5

您可以使用自定义 saga 并changeredux-form. 这样说听起来有点复杂,但实际上并非如此。:)

首先,在您的应用程序中实现自定义 sagas:

const customSagas = [
    MyCustomEntitySagas,
];

const App = () => (
    <Admin customSagas={customSagas}>
        // ...
    </Admin>
);

然后,在您的自定义 saga 中,只需观察您将从 中发出的动作CustomField,例如:

import { change } from 'redux-form';
import { put, takeEvery } from 'redux-saga/effects';

import { CUSTOM_FIELD_CHANGE } from './actions';

export function* watchForCustomFieldChange({ payload }) {
    yield put(change('form-record', 'title', payload.newTitle));
}

export default function* root() {
    yield [
        takeEvery(CUSTOM_FIELD_CHANGE, watchForCustomFieldChange),
    ];
}

例如,这是一个您可能想要调度的动作创建者:

export const CUSTOM_FIELD_CHANGE = 'CUSTOM_FIELD_CHANGE';

export const customFieldChange = (newTitle) => ({
    type: CUSTOM_FIELD_CHANGE,
    payload: { newTitle },
});

这里的关键是Redux Sagas。看起来像很多样板,但它确实简化了最终的开发。

我试图尽可能短,以防止过多的噪音。但请随时询问您是否需要更多信息。:)

于 2017-03-17T15:50:02.783 回答