0
夥計們。異步動作創建器不按預期調度動作
我有以下異步動作創建者調度另一個動作創建者或返回null
取決於當前狀態。問題在於無論現狀如何,它總是返回null
。
// actions.js
export const loadPosts = category => (dispatch, getState) => {
const currentState = getState()
const posts = currentState.postsByCategory
const categoryPosts = posts[category]
const items = categoryPosts.items
if(items === []) {
return dispatch(fetchPosts(category))
}
return null
}
正如你可以看到,動作創建者分派fetchPosts()
取決於items
等於空數組的值。我測試它爲它提供以下結構的INTIAL狀態:
// initialState
const initialState = {
postsByCategory: {
hot: {
isFetching: false,
items: []
}
}
}
我顯然不恰當地訪問items
財產,但我不能看到我的代碼錯誤的。 我正在用redux-mock-store來測試它,創建一個mockStore的實例,併爲它提供initialState
。 希望你們能在我的代碼中找出錯誤。 在此先感謝。
嘿人,它的工作。謝謝!我只是不太明白=== []和使用.length === 0.之間的區別。它與數組元數據有什麼關係?你有任何建議進一步閱讀,以更好地掌握這一點? –