首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ES6方法返回数据?

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

Stack Overflow用户
提问于 2017-10-12 14:24:21
回答 1查看 215关注 0票数 0

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

根据我的代码,结果应该是正确的或错误的。

我的密码

get_gathereddata_status.js

代码语言:javascript
复制
 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

代码语言:javascript
复制
function mapStateToProps({ experiment }) {
  const { selectedTab } = experiment.tabs;
  const isGatherDataEnabled = gatherDataStatus();
  console.log(isGatherDataEnabled);
  return {
    selectedTab,
    isGatherDataEnabled
  };
}

console.log in mapStateToProps

代码语言:javascript
复制
ƒ (dispatch, getState) {
    var _getState = getState(),
        experiment = _getState.experiment;

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

回答 1

Stack Overflow用户

发布于 2017-10-12 14:35:26

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

代码语言:javascript
复制
() => (dispatch, getState) => { //...}

所以当你用:

代码语言:javascript
复制
const isGatherDataEnabled = gatherDataStatus(); 
// assuming gatherDataStatus this is the same as get_gathereddata_status.js

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

我认为您只想导出第二个函数:

代码语言:javascript
复制
export default (dispatch, getState) => { //.. }

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

代码语言:javascript
复制
console.log(isGatherDataEnabled(dispatch, getState));
// You need to call this with the expected arguments. Where do these come from?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46712121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档