【问题标题】:Integration (Redux with react native)集成(Redux 与本机反应)
【发布时间】:2023-03-16 23:18:01
【问题描述】:

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

【问题讨论】:

标签: react-native redux


【解决方案1】:

您需要使用 redux-saga 或 redux-thunk 来处理这种情况。 你可以在这里找到例子 Redu-saga Redux-thunk

【讨论】:

    【解决方案2】:

    你不需要 redux-thunk 来处理你可以使用 async/await 的 Promise 并且在你的 Promise 解决后你可以发送新数据;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 2022-10-18
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多