React Native FlatList没有取消Axios API请求

时间:2019-03-12 16:20:00

标签: android react-native redux redux-thunk react-native-flatlist

我是本机的新手,我在处理道具和状态时遇到了问题,当我使用redux时,我获得了以正确格式呈现平面列表所需的数据,但是其中一些数据属性如何平面列表仅将{this.props.customers}视为未定义。 这是我的代码:

  componentWillMount() {
    debugger;
    this.props.getCustomers();
    debugger;
  }

  componentDidUpdate(prevProps) {
    if (prevProps.customers !== this.props.customers) {
      this.props.getCustomers();
    }
  }


  render = () => {
    return (
      <View>
        <FlatList
          data={this.props.customers}
          renderItem={({ item }) =>
            <View style={styles.GridViewContainer}>
              <Text style={styles.GridViewTextLayout}>{item.name}</Text>
            </View>}
          keyExtractor={(x, index) => index}
          numColumns={3}
        />
      </View>
    );
  }
}

const mapStateToProps = (state) => {
  const customers = state.customers;
  console.log(customers);
  debugger
  return customers;

};


export default connect(mapStateToProps, {
  getCustomers
})(CustomersList);

和getCustomers动作:

export const getCustomers = () => {
    debugger;
    return (dispatch) => {
        dispatch(setCustomerLoading)
        axios
            .get('https://calm-sands-26165.herokuapp.com/api/customers')
            .then(res =>
                dispatch({
                    type: GET_CUSTOMERS,
                    payload: res.data,
                })
            )
            .catch(err =>
                dispatch({
                    type: GET_ERRORS,
                    payload: null
                })
            );
    };
}

先感谢

0 个答案:

没有答案