小编sam*_*lam的帖子

在Redux Reducer中执行Ajax Fetch?

我正试图绕过Redux actionCreators中的状态; 而是执行以下操作(在reducer中执行ajax操作).为什么我需要为此访问状态 - 因为我想使用存储在状态中的CSRF令牌执行ajax.

有人可以告诉我,如果以下被认为是不良做法/反模式?

export const reducer = (state = {} , action = {}) => {

    case DELETE_COMMENT: {

        // back-end ops
        const formData = new FormData();
        formData.append('csrf' , state.csrfToken);
        fetch('/delete-comment/' + action.commentId , {
            credentials:'include' ,
            headers:new Headers({
                'X-Requested-With':'XMLHttpRequest'
            }) ,
            method:'POST' ,
            body:formData
        })

        // return new state
        return {
            ...state ,
            comments:state.comments.filter(comment => comment.id !== action.commentId)
        };
    }

    default: {
        return state;
    }
}
Run Code Online (Sandbox Code Playgroud)

redux redux-thunk

3
推荐指数
1
解决办法
2016
查看次数

标签 统计

redux ×1

redux-thunk ×1