如何使用ES6方法返回数据?

时间:2017-10-12 14:24:21

标签: javascript reactjs ecmascript-6 redux

我试图使用ES6函数返回数据,但它返回的是函数而不是结果。

根据我的代码

,结果应该是true或false
  

我的代码

get_gathereddata_status.js

 export default () => (dispatch, getState) => {
  const { experiment } = getState();
  const { selectedTab, gatherData } = experiment.tabs;
  const { environmentalChanges: { environmentFactor, environmentLocation } } = experiment;
  const { populationChanges: { populationlLocation, populationFactor } } = experiment;
  if (selectedTab === 'tab1') {
    return environmentFactor !== '' && environmentLocation !== '' && !gatherData[selectedTab];
  } else if (selectedTab === 'tab2') {
    return populationlLocation !== '' && populationFactor !== '' && !gatherData[selectedTab];
  }
  return false;
};

mapStateToProps

function mapStateToProps({ experiment }) {
  const { selectedTab } = experiment.tabs;
  const isGatherDataEnabled = gatherDataStatus();
  console.log(isGatherDataEnabled);
  return {
    selectedTab,
    isGatherDataEnabled
  };
}
mapStateToProps中的

console.log

ƒ (dispatch, getState) {
    var _getState = getState(),
        experiment = _getState.experiment;

    var _experiment$tabs = experiment.tabs,
        selectedTab = _experiment$tabs.selectedTab,

1 个答案:

答案 0 :(得分:0)

get_gathereddata_status.js返回一个返回另一个函数的函数:

() => (dispatch, getState) => { //...}

所以当你用:

打电话时
const isGatherDataEnabled = gatherDataStatus(); 
// assuming gatherDataStatus this is the same as get_gathereddata_status.js

isGatherDataEnabled现在是gatherDataStatus()返回的函数,而不是第二个函数的结果。

我想你只想导出第二个函数:

export default (dispatch, getState) => { //.. }

如果你确实需要,可以调用返回的函数:

console.log(isGatherDataEnabled(dispatch, getState));
// You need to call this with the expected arguments. Where do these come from?