javascript - react中用了async就不用redux-thunk这个插件了吧?
本文介绍了javascript - react中用了async就不用redux-thunk这个插件了吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
export const getMenu = () => {
return async (dispatch) => {
let msg = await fetch('data/nodes',{
method: 'POST',
//body: JSON.stringify({"aa":"aa"}),
header: {'content-type':'application/json; charset=utf-8'}
}).then((res)=>res.json());
var state = { nodes: msg };
dispatch({
type: 'INIT',
state
});
};
}
解决方案
但是你这样写还是需要的,redux-thunk就是支持Function Action。
这种你想在ActionCreator 中自己dispatch,还是要用这种方法。
如果你的Action诗歌Promise,你可以换成 redux-promise,他会自动帮你dispatch resolve后的值。
这篇关于javascript - react中用了async就不用redux-thunk这个插件了吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文