在React App中通过Redux调用API无效

问题描述 投票:1回答:1

我正在使用MERN堆栈构建一个Web应用,并使用redux作为状态管理。我现在正在尝试使用我的API从mongoDB加载数据并存储到redux中,然后再设置为反应状态。直接从React应用程序使用getShop()函数调用api时,数据以React状态存储并显示在应用程序中。但是,当我尝试根据我的以下代码通过redux使用它时,它不起作用。没有错误,只是没有加载任何信息。

动作:

load : () => {
       let thunk = (dispatch) => {
           api.getShops()
           .then(res => {
               let barbershops = res.data
               dispatch(barberFactory.set(barbershops))
           })
       }
       return thunk
   },

从react应用调用:

function mapDispatchToProps(dispatch){
  return {
    loadBarber : () => {
      dispatch(barberFactory.load)
    },
  }
}

该函数在componentDidMount()处调用

reactjs mongodb redux react-redux redux-thunk
1个回答
0
投票
dispatch(barberFactory.load())
© www.soinside.com 2019 - 2024. All rights reserved.