Redux嵌套的thunk,从两个不同的端点获取

时间:2019-06-19 10:42:00

标签: react-native redux-thunk

我想结合thunks来从wordpress获取应用程序的初始数据,然后从另一个端点获取tags

这是我获取posts(视频)json数据的第一个动作:

export function fetchData() {
    const url = "http://app.rguc.co.uk/?rest_route=/wp/v2/posts";

    return dispatch => {
        fetch(url)
            .then(response => {
                return response;
            })
            .then(response => response.json())
            .then(data => {
                if (data) {
                    dispatch(fetchSuccessVideos(data));
                } else {
                    dispatch(fetchErrorVideos(data));
                }
            })
            .catch(error => {
                console.log(error);
            });
    };
}

对于每个帖子,我想使用帖子json中的标签数组来获取标签名称:

tags: [12]

export function getTag(id) {
    const url = "http://app.rguc.co.uk/?rest_route=/wp/v2/tags/" + id;
    return dispatch => {
        fetch(url)
            .then(response => {
                return response;
            })
            .then(response => response.json())
            .then(data => {
                if (data) {
                    dispatch(fetchSuccessTags(data));
                } else {
                    dispatch(fetchErrorTags(data));
                }
            })
            .catch(error => {
                console.log(error);
            });
    };
}

当我合并它们时,如何为每个帖子触发fetch

export function getDataAndTag() {

    return (dispatch, getState) => {

        return dispatch(fetchData()).then(() => {

            // For each post get tag names
            return dispatch(getTag(tagId));
        });
    };
}

0 个答案:

没有答案