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



我是Redux的新手,我正在向Spotify API发送API请求。我正在使用Thunk,并有一个名为FetchCurrentlyPlaying的操作,它可以获取正在播放的当前歌曲。

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

FetchAlbums将向Spotify API发送另一个API请求,并将当前播放艺术家的id传递给URL。这将检索该艺术家的其他相册。

如何将id从一个操作传递到另一个操作?

您可以从thunk 中调度其他操作

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));
});

最新更新