React/Redux - 使用 Thunk 将 JSON 数据从一个 API 调用传递到另一个 Redux 操作
React/Redux - Passing JSON data from one API call to another Redux action using Thunk
我是 Redux 的新手,我正在向 Spotify API 发送 API 请求。我正在使用 Thunk 并有一个名为 FetchCurrentlyPlaying 的操作,它获取正在播放的当前歌曲。
在此 API 调用返回的 JSON 中,有一个标识艺术家的 ID 号。当我在 FetchCurrentlyPlaying 中检索此 JSON 数据时,我想将此 ID 号传递给另一个 Redux 操作 FetchAlbums.
FetchAlbums 然后会向 Spotify API 发送另一个 API 请求,并向 URL 传递当前正在播放的艺术家的 ID。这将检索该艺术家的其他专辑。
如何将 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));
});
我是 Redux 的新手,我正在向 Spotify API 发送 API 请求。我正在使用 Thunk 并有一个名为 FetchCurrentlyPlaying 的操作,它获取正在播放的当前歌曲。
在此 API 调用返回的 JSON 中,有一个标识艺术家的 ID 号。当我在 FetchCurrentlyPlaying 中检索此 JSON 数据时,我想将此 ID 号传递给另一个 Redux 操作 FetchAlbums.
FetchAlbums 然后会向 Spotify API 发送另一个 API 请求,并向 URL 传递当前正在播放的艺术家的 ID。这将检索该艺术家的其他专辑。
如何将 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));
});