我想向我的 Action 生成器添加一个 isLoading 标志,并在我的 reducer 上重置它。最初没有标志,我的代码可以工作,操作如下所示
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,
}
}
reducer 看起来像
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
}
default:
return state;
}
}
如您所见,该对象返回正常,我可以通过 action.payload.data.data.results 获取我的列表
请注意,我使用 redux Promise 作为中间件来处理 Promise。
一旦我将操作更改为以下内容并重新运行代码,我的有效负载(如下图所示)就会成为 Promise,而不是返回的对象
export function getComicList() {
const FIELD = '/comics'
let searchUrl = ROOT_URL + FIELD + '?ts=' + TS + '&apikey=' + PUBLIC_KEY + '&hash=' + HASH;
const request = axios.get(searchUrl)
return {
type: FETCH_COMIC_LIST,
isLoading: true,
payload: request,
}
}
为什么简单地添加另一个变量就会导致这个问题?
最佳答案
Redux Promise 和 Redux Promise 中间件均符合 Flux Standard Action (FSA) 。向操作添加一个 meta
键并在其中分配您的键/值对。
相关问题/答案:https://stackoverflow.com/a/35362148/4290174
来自 FSA 文档:
The optional meta property MAY be any type of value. It is intended for any extra information that is not part of the payload.
关于reactjs - React Redux 添加额外的操作字段导致 promise 以不同的方式返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672996/