我有这个MapStateToProps:
const mapStateToProps = (state) => {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const ESFReport = getESFReport(state);
const resultStatusReport = getResultStatusReport(state);
return {
companInfo: getCompanyInfo(state),
companyId: getCompanyId(state),
...ESFReport,
...resultStatusReport,
year,
month,
};
};
而...ESFReport,
和...resultStatusReport
,都有相同的属性:report
但我需要以某种方式更改名称,因为在同一组件中我使用const { report } = this.props
两次但不同的道具。
我怎样才能做到这一点? (当我只有... ESFReport,它曾经工作,但当我添加... resultStatusReport,它打破了)。
提前致谢。
如果你不需要report
和ESFReport
对象的resultStatusReport
以外的任何东西你可以做:
const mapStateToProps = (state) => {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const { report: ESFReport } = getESFReport(state);
const { report: resultStatusReport } = getResultStatusReport(state);
return {
companInfo: getCompanyInfo(state),
companyId: getCompanyId(state),
ESFReport,
resultStatusReport,
year,
month,
};
};
它只是将report
属性重命名为ESFReport
和resultStatusReport
。然后你会有this.props.ESFReport
,实际上是ESFReport.report
和this.props.resultStatusReport
,这将是resultStatusReport.report
。