首页 > 解决方案 > 如何使用来自 api 的 redux 获取数据?

问题描述

嗨,我的朋友们,我将使用 redux 从 api 获取数据 如果您需要更改代码,我的代码可以在下面的链接中找到

这是我的项目链接:

https://codesandbox.io/s/getdatafromapiwhithaxiosusingreduxinreactproject-s8ckq

我收到了来自 api 的数据,但如何在组件中使用它?

标签: reactjsreact-redux

解决方案


您尚未创建Reducer.js文件

您需要创建该文件,并在其中包含一个减速器。

使用导出减速器export default reducer;

将数据放入您的 props 对象后,例如products,您需要像这样映射它:

class App extends Component {
  render() {
    return {
        this.props.products.map((product, i) => (
            <li>product.title</li>
        ))
    }
  }
}

推荐阅读