React Native FlatList 未呈现 Axios API 请求
React Native FlatList is not rendring an Axios API request
我是 React Native 的新手,我在处理 props 和状态时遇到了问题,当我使用 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
})
);
};
}
提前致谢。
在 mapStateToProps 中,您应该 return 一个对象,而不是一个值。该对象中的每个条目都将是连接到商店的组件的道具。
对于您的情况,这应该是解决方法:
const mapStateToProps = (state) => {
const customers = state.customers;
return { customers };
};
我是 React Native 的新手,我在处理 props 和状态时遇到了问题,当我使用 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
})
);
};
}
提前致谢。
在 mapStateToProps 中,您应该 return 一个对象,而不是一个值。该对象中的每个条目都将是连接到商店的组件的道具。
对于您的情况,这应该是解决方法:
const mapStateToProps = (state) => {
const customers = state.customers;
return { customers };
};