首页 > 解决方案 > React 尝试将异步调用传递给 Props

问题描述

所以我要做的基本上是调用一个异步函数,而不是要求 mapStateIntoProps 将它传递给 props 到实际组件中。当我这样做时,我得到一个 console.log() ,它显示我的数据在里面。

here is my first file that has the async func
   export const getIdMovie = async (state,movieId)=>{
  let data= await axios
  .get(
    `https://api.themoviedb.org/3/movie/${movieId}? 
      api_key=${APIKEY}&language=en-US`
  )
  let results=data.data

  return results
}

this is where i try to call it on the second file
injectDataReducer(store, { key: "movie", reducer: MovieReducer });

const mapStateToProps = (state, ownProps) => ({
  movie: getIdMovie(state,ownProps.movieId)
});

这是我的 console.log

标签: reactjsreduxreact-props

解决方案


如果 getIdMovie 是一个动作创建者,您将不得不使用redux-thunk。Reducer 在您调度和动作时异步更新存储,以避免多个调度动作更改相同的数据。

````此外,您必须首先将状态(即电影)设置为 reducer,然后将数据从那里更新到您的组件中。```


推荐阅读