【发布时间】:2019-05-08 14:49:09
【问题描述】:
我创建了一个Custom Hook。在这个钩子中,我收到了来自loadRates() 的 Promise:
export const actions = {
updateRate: () =>
loadRates()
.then(response => ({
type: types.UPDATE_RATE,
value: response
}))
.catch(reject => ({
type: types.ERROR_UPDATE_RATE,
value: reject
}))
};
export const reducer = (state, action) => {
switch (action.type) {
case types.UPDATE_RATE:
return { ...state, rates: action.value, loading: false };
case types.ERROR_UPDATE_RATE:
return { ...state, error: action.value, loading: false };
default:
throw new Error("Action type must be defined");
}
};
问题是即使我有来自loadRates() 的response,我也会收到Action type must be defined 错误,因为我的state 是pending。
我应该怎么做才能纠正这个问题?
提前致谢!
【问题讨论】:
-
你的自定义钩子在哪里?
-
我认为您错过了为您的操作定义 action.type 。据我所知,这是一个关于 redux 操作的问题,而不是 react-hooks 或 promises
-
这是我的自定义钩子。问题是它没有等待
actions而是转到reducer。所以,因为我的state是pending,它会抛出错误。action.type在export const actions中定义,在loadRates().then和loadRates().catch内部 -
你能用它做一个有效的小提琴吗?
-
好的,会做的!
标签: javascript reactjs react-hooks