代码之家  ›  专栏  ›  技术社区  ›  Nick Kinlen

React/Redux-使用Thunk将JSON数据从一个API调用传递到另一个Redux操作

  •  0
  • Nick Kinlen  · 技术社区  · 6 年前

    在这个API调用返回的JSON中,有一个标识艺术家的id号。当我在FetchCurrentlyPlaying中检索这个JSON数据时,我想把这个id号传递给另一个Redux操作FetchAlbums。

    FetchAlbums随后会向spotifyapi发送另一个API请求,并将当前播放艺术家的id传递给URL。这将检索该艺术家的其他专辑。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Asaf Aviv    6 年前

    你可以从你的重击中发送其他动作

    const FetchAlbums = albumId => dispatch => axios(albumId)
      .then((data) => {
        dispatch(setAlbum(data));
      });
    
    const FetchCurrentlyPlaying = song => dispatch => axios(song)
      .then((data) => {
        dispatch(setSong(data));
        dispatch(FetchAlbums(data.albumId));
      });