React Native Flatlist不会提出Axios API请求



我是对本机的新手但是,单位列表中的数据属性仅如何显示{this.props.customers} as undewined。这是我的代码:

  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
                })
            );
    };
}

thanx提前。

在mapstatetoprops中,您应该返回对象,而不是一个值。该对象中的每个条目将是要连接到商店的组件的道具。

在您的情况下,这应该是解决方案:

const mapStateToProps = (state) => {
  const customers = state.customers;
  return { customers };
};

相关内容

  • 没有找到相关文章

最新更新