Integration (Redux with react native)



我在应用程序中使用reduxreact-native。我从 API 获取了类别的所有值,并希望将 categoryId 传递给 action.js 文件。 但是我在将获取的数据传递到操作.js页面时遇到问题。任何人都可以给我一个可靠的 redux 示例,用于从 API 获取数据并将其传递给 action#flatlist 和 #redux

您需要使用 redux-saga 或 redux-thunk 来处理这种情况。 您可以在此处找到示例 雷杜-萨加 Redux-thunk

你不需要 redux-thunk 来处理你可以使用 async/await 的承诺,在你的承诺解决后,你可以调度新数据;

import React from 'react';
import { connect } from 'react-redux';
class MyComponent extends React.Component {
constructor() {
this.fetchItems = this.fetchItems.bind(this);
}
async fetchItems() {
const response = await api('api/items');
this.props.dispatch('items', response.data);
}
}
const mapStateToProps = (state) => state;
const mapDispatchToProps = (dispatch) => dispatch;
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);

最新更新