在Redux中,Promise.resolve(dispatch(null))可能吗?
我试图调度null以创建动作?这可能吗?
答案 0 :(得分:0)
这在redux中是不可能的。如redux文档所述,只有在操作是纯对象且操作类型未定义时才可以分派操作。
这是redux / src / createStore.js文件中调度功能的代码段。
function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}
if (isDispatching) {
throw new Error('Reducers may not dispatch actions.')
}
try {
isDispatching = true
currentState = currentReducer(currentState, action)
} finally {
isDispatching = false
}
const listeners = currentListeners = nextListeners
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i]
listener()
}
return action
}
与其像您建议的那样进行分发,不如通过一个更好的实施方式
Promise.resolve(dispatch({ type: 'YOUR_SAMPLE_ACTION_TYPE', data: null }))
我希望它会有所帮助。谢谢!