反应试图将异步调用传递给道具

时间:2020-03-20 06:05:47

标签: reactjs redux react-props

所以我想做的基本上是调用async函数,而不是要求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)
});

here is my console.log

1 个答案:

答案 0 :(得分:0)

如果getIdMovie是操作创建者,则必须使用redux-thunk。Reducer在分派和操作时异步更新存储,以避免通过多个分派操作更改相同的数据。

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