【发布时间】:2017-02-05 07:56:50
【问题描述】:
我正在使用 redux 和 ES6 在 react.js 中工作。我有一个导出函数的操作文件,如下所示:
MonthlyRevenueActions.js
export function fetchMonthlyRevenue(name, startDate, endDate) {
return function(dispatch) {
dispatch(console.log("name": " + name + ", start: " + startDate + ", end: " + endDate));
}
}
然后在我的容器代码中,我有一个 import 语句并尝试使用该函数,但我可以在 Chrome DevTools 中看到该变量的值为 0:
MonthlyDisplayContainer.js:
import {otherFunc, fetchMonthlyRevenue, otherFunctionTwo} from 'actions/monthly/MonthlyRevenueActions'
const mapStateToProps = (state, ownProps) => {
//... other code that's being executed
fetchMonthlyRevenue(name, startDate, endDate);
}
我可以看到 name、startDate 和 endDate 的值,但 fetchMonthlyRevenue() 函数只有 0。
我希望我在作用域方面遗漏了一些东西(我是 ES6 新手),但我终生无法弄清楚如何执行此功能。
提前感谢您的帮助。
【问题讨论】:
-
我不明白那行代码。你正在执行一个函数,它返回一个函数,并且对返回值什么都不做?是的,0 很奇怪,但是......代码也是......
-
您遇到的错误是什么?
-
它没有给出错误......它的代码运行通过,没有给出任何错误。如果我将
console.log()放入其中,则会在fetchMonthlyRevenue()行旁边打印出来。 -
所以......虽然没有给出明确的“错误”本身,但我显然试图错误地导入/使用该函数......但我很困惑为什么会发生这种情况。在这个文件的底部(在
mapToProps()函数之外,fetchMonthlyRevenue()方法正确执行)
标签: reactjs ecmascript-6 redux react-redux