【发布时间】:2019-01-03 09:47:46
【问题描述】:
首先,我想说我是 react 的初学者(我讨厌前端开发,但你知道,有时我们不会在工作生活中选择)......
所以,我使用 react-admin 创建了一个自定义表单,而不使用 react-admin 的 REST 连接(它是一个特定的表单)。
表单验证后,几个数据的一个名为 processingStatut 的值发生了变化,需要在表单中显示这个新值
<List><Datagrid> 由 react-admin 映射。
所以我按照文档创建了一个 reducer 操作来更改我的 dataGrid 中名为 processingStatut 的布尔值,如下所示:
epassesReceived.js
export const EPASSES_RECEIVED = 'EPASSES_RECEIVED';
export const epassesReceived = (data) => ({
type: EPASSES_RECEIVED,
payload: { data },
});
我的 customForm.js
import { epassesReceived as epassesReceivedAction } from './epassesReceived';
handleSubmit(event) {
this.setState({
post: this.post
});
const { fetchJson } = fetchUtils;
const {
showNotification,
history,
push,
epassesReceived,
fetchStart, fetchEnd
} = this.props;
const url = `${API_URL}/ePasses/update`;
const datas = JSON.stringify(this.state);
const options = {
method: 'POST',
body: datas
};
fetchStart();
fetchJson(url, options)
.then( response => epassesReceived(response.json) )
.then(() => {
showNotification('ra.notification.epasseRecorded');
history.goBack();
})
.catch( error => {
console.error(error);
var message = error.message.replace(/ /g, '');
showNotification(`ra.notification.${message}`, 'warning');
})
.finally(fetchEnd);
event.preventDefault();
}
...
const mapStateToProps = state => ({
customReducer: state.customReducer
});
export const EpassesUpdate = connect(mapStateToProps, {
epassesReceived: epassesReceivedAction,
showNotification,
push,fetchStart, fetchEnd
})(translate(withStyles(formStyle)(EpassesUpdateView)));
在我的 app.js 中
import { EPASSES_RECEIVED } from './epassesReceived';
const customReducer = (previousState = 0, { type, payload }) => {
console.log(payload, type);
if (type == EPASSES_RECEIVED) {
// console.log('modif');
// payload.data[0].processingStatut=1; this is the purpose of the script. To show de modification changed after form's validation
return payload;
}
return previousState;
}
和 viewDataGrid.js
<List
classes={props.classes}
{...props}
exporter={exporter}
title='ePass.pageTitle'
perPage={15}
pagination={<PostPagination />}
filters={<EPassFilter businessunit={businessUnit} />}
bulkActions={<EPassBulkActions businessunit={businessUnit} />}
actions={<PostActions businessUnit={businessUnit} />}
>
<Datagrid classes={props.classes}>
{ businessUnit === undefined || !businessUnit.companyName &&
<TextField source="businessUnitName" label="ePass.businessUnitName" />
}
<StateField source="processingStatut" label="" translate={props.translate} />
.....
但是在我的控制台日志中,我的值没有改变,我现在也不知道为什么......当然,如果我按 F5 刷新我的网页,它会起作用,因为我的数据库中的值已更改。但不在 react 的 dataGrid 中……我迷路了……
也许日志输出会有所帮助:
我们可以看到类型“EPASSES_RECEIVED”并且数据发生了变化
【问题讨论】:
标签: javascript reactjs ecmascript-6 react-admin