вы сказали, что вам нужно дождаться createPlaylist
to fini sh выборки данных, так почему бы не объединить 2 функции в одну, например:
export const createAndFillPlaylist = () => async (dispatch, getState) => {
try {
const state = getState();
let uris = state.recommendations.map(item => item.uri);
const PlayListBody = JSON.stringify({
name: "Playlist Name",
description: "Playlist description"
});
const headers = {
Authorization: "Bearer " + state.token,
"Content-Type": "application/json"
};
const { data } = await spotify.post(
`/users/${state.user.id}/playlists`,
PlayListBody,
{ headers }
);
dispatch({ type: "CREATE_PLAYLIST", payload: data });
const createPlaylistBody = JSON.stringify({
uris: uris
});
const response = await spotify.post(
`/playlists/${data.id}/tracks`,
createPlaylistBody,
{
headers
}
);
dispatch({ type: "FILL_PLAYLIST", payload: response.data });
} catch (error) {
throw error;
}
};
и для компонента не требуется ToMapStateToProps
, вот код:
import React, { Component } from "react";
import { connect } from "react-redux";
import { createAndFillPlayList } from "../actions";
class Result extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<div>
<h1>Result</h1>
<button onClick={this.props.createAndFillPlayList}>
Create Playlist
</button>
</div>
);
}
}
export default connect(
null,
{ createAndFillPlayList }
)(Result);