mapDispatchToProps,id未在操作中定义

时间:2018-04-06 15:45:58

标签: react-native redux

尝试使用fetchEvents作为参数时,button.value中的 ID未定义

我在我的组件中使用mapDispatchToPropsmapStateToProps

const mapDispatchToProps = (dispatch) => {
  return {
    resetForm: () => dispatch(resetForm()),
    fetchEvents: setSubCategory => dispatch(fetchEvents(id))
  };
};

const mapStateToProps = state => {
  return {
    setCredentials: state.setCredentials,
    categories: state.fetchCategories,
    isLoading: state.isLoading
  };
};

然后我解构我的道具以获得我的id

const {
  fetchEvents,
  resetForm,
  isLoading,
  setCredentials: { setStudent, setGroup, setYear }
} = this.props;

const id = setStudent || setGroup || setYear;

然而,当我发出行动时:

 const buttonOptions = [
      {
        key: 0,
        label: "refresh",
        value: Id,
        icon: "undo"
      },
      {
        key: 1,
        label: "back",
        value: Id,
        icon: "caret-left"
      }
    ];

    return (
      <View style={styles.container}>
        {buttonOptions.map((button, i) => {
          const style =
            i == 0 ? styles.divContainerLeft : styles.divContainerRight;

          return (
            <View style={style} key={"view" + i}>
              <TouchableOpacity
                disabled={isLoading ? true : false}
                key={"TouchableOpacity" + i}

                // dispatch action here
                onPress={i == 0 ? () => fetchEvents(button.value) : resetForm}
              >
                <Icon
                  name={button.icon}
                  style={styles.button}
                  color="white"
                  key={"icon" + i}
                  size={30}
                />
              </TouchableOpacity>
            </View>
          );
        })}
      </View>
    );

1 个答案:

答案 0 :(得分:0)

<div class="intro-container"> <div class="intro-container-text"></div> <div class="intro-container-img"></div> <div class="clear"></div> </div> .clear { clear: both; } 中,您要将参数名称重新声明为mapDispatchToProps,但在您的提取通话中,您会传递setSubCategory

相反,试试这个:

id

OR

const mapDispatchToProps = (dispatch) => {
  return {
    resetForm: () => dispatch(resetForm()),
    fetchEvents: (setSubCategory) => dispatch(fetchEvents(setSubCategory))
  };
};

这是因为您将const mapDispatchToProps = (dispatch) => { return { resetForm: () => dispatch(resetForm()), fetchEvents: (id) => dispatch(fetchEvents(id)) }; }; 声明并内联为匿名函数。参数名称必须匹配。