guys.
I have the following async action creator that dispatches another action creator or returns null
depending on the current state. The problem is that it is invariably returning null
no matter what the current state is.
// 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
}
As you can see, the action creator dispatching fetchPosts()
depends on the value of items
being equal to an empty array. I am testing it providing it an intial state with the following structure:
// initialState
const initialState = {
postsByCategory: {
hot: {
isFetching: false,
items: []
}
}
}
I am obviously not accessing the items
property appropriately, but I cannot see where the error in my code is.
I am testing this with redux-mock-store, creating an instance of a mockStore, and providing it with initialState
.
Hope you guys can pinpoint the error in my code.
Thanks in advance.
Your problem lies in the comparison
if(items === [])
In the above code, items will not be ===
to []
as they both are of different instances. If you want to check if items
is empty, please use items.length === 0
. Hope that helps.