我使用过Redux Promise,但似乎Redux Promise Middleware具有更多功能,例如通过附加"PENDING"或"FULFILLED"来调度多个动作.
为什么我会使用一个而不是另一个?
我搜索过高低,但找不到明确的答案。
我设法将注意力集中在Redux的机制上,但是当我谈到API调用和异步操作创建者时,我就在Promises的上下文中停留在中间件上。
你能帮我弄好乱子吗?
这个难题的矛盾部分让我头疼:
YT教程之一说,本机Redux分派方法不支持从动作创建者返回的诺言-因此需要Redux Promise库(我知道该项目现在可能已经死了,并且继续是Redux Promise中间件)。
Dan在“ redux-thunk和redux-promise之间有什么区别? ”中说,即使没有中间件,我也可以使用诺言-只需在动作创建者中对其进行管理即可。
在其他答案中,我找到了使用thunk的示例,其中动作创建者返回了一个... promise(后来在调用者中处理/ dispatch(myActionCreator(params).then(...) /所以一个promise 可以由a返回)没有任何redux-promise lib的thunk 。
在“ redux-thunk和redux-promise之间有什么区别? ”中,接受的答案指出Redux Thunk返回函数,而Redux Promise返回promise ..到底是什么?
总结一下:使用Redux Promise或Redux Promise中间件有什么意义?为什么仅Redux不能原生支持诺言?
更新:
我刚刚意识到,在3点以上我忽略then()被连接到dispatch不包括在dispatch()ARGS。
我想在我的动作生成器中添加一个 isLoading 标志并在我的减速器上重置它。最初没有标志,我的代码工作,动作如下所示
export function getList() {
const FIELD = '/comics'
let searchUrl = ROOT_URL + FIELD + '?ts=' + TS + '&apikey=' + PUBLIC_KEY + '&hash=' + HASH;
const request = axios.get(searchUrl)
return {
type: FETCH_LIST,
payload: request,
}
}
Run Code Online (Sandbox Code Playgroud)
和减速机看起来像
const INITIAL_STATE = { all: [], id: -1, isLoading: false };
export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case FETCH_COMIC_LIST:
console.log('reducer action =', action, 'state =', state)
return {
...state,
all: action.payload.data.data.results,
isLoading: false …Run Code Online (Sandbox Code Playgroud) 我正在使用 redux-promise-middleware 和 redux-thunk 来链接我的承诺:
import { Dispatch } from 'redux';
class Actions {
private static _dispatcher: Dispatch<any>;
public static get dispatcher(): Dispatch<any> {
return Actions._dispatcher;
}
public static test() {
this.dispatcher({
type: 'MY_ACTION',
payload: new Promise(resolve => resolve('hi'));
}).then(result => {
console.log(result); // this works
});
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但在编译时也会生成警告:
TS2339:类型“{ type: string;”上不存在属性“then” 有效载荷:承诺<{}>;}'
听起来我需要将Promise<...>某处作为类型包含在内,以便打字稿知道这then实际上是返回的对象上的属性,dispatcher()但我无法消除错误。
https://github.com/gaearon/redux-thunk/issues/103
import { Dispatch } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { getStore, IState } from …Run Code Online (Sandbox Code Playgroud) javascript typescript redux redux-thunk redux-promise-middleware
所以基本上我正在使用 thunk 和 redux-promise-middleware 调度一个动作,这使得 API 调用返回一个承诺。然后我将返回的承诺作为“有效载荷”参数发送给另一个动作创建者,该参数与 redux-promise-middleware 一起使用并处理不同的动作 MY_ACTION_TYPE_PENDING 或 MY_ACTION_TYPE_REJECTED 或 MY_ACTION_TYPE_FULFILLED。我的问题是我是否通过 _REJECTED 操作处理 reducer 中的错误,而不是在我的 dispatch(actionCreator(payload)? _REJECTED ACTION 错误。
下面是我的一些操作:
export const RECEIVE_POSTS = 'RECEIVE_POSTS';
export const receivePosts = (data) => ({
type: RECEIVE_POSTS,
payload: data
})
// thunk middleware for fetching blog
export const fetchPosts = () => {
return (dispatch) => {
const payload = contentfulClient.getEntries().then(
data => data.items,
error => console.log('An error occurred in fetchPost thunk middleware', error)
)
return dispatch(receivePosts(payload))
.catch((error) => { …Run Code Online (Sandbox Code Playgroud) javascript reactjs es6-promise redux redux-promise-middleware
我正在使用Axios,Redux和Redux Promise Middleware.
我有以下动作创建者:
return {
type: 'FETCH_USERS',
payload: axios.get('http://localhost:8080/users',
{
params: {
page
}
}
)
}
Run Code Online (Sandbox Code Playgroud)
在我的FETCH_USERS_PENDING动作减速器中,我想在我的状态中保存请求的URL.我怎样才能做到这一点?
redux ×5
javascript ×3
reactjs ×2
redux-thunk ×2
axios ×1
es6-promise ×1
react-redux ×1
typescript ×1