I'm trying to figure out what is the best way to fetch an item if it's not in the store.
For instance, let say that I have a list of users and I want to fetch a specific user. If that user isn't in the store I want to fetch him from the DB.
Is this code below considered best practice or is there a better way doing so?
const searchedUserId = 'abcdefg';
this.selecedUser$ = this.store.select(state => {
const selectedUserIndex = state.userList.findIndex(searchedUserId)
if (selectedUserIndex === -1) {
this.store.dispatch(new UsersActions.FetchUsers(searchedUserId));
} else {
return state.userList[selectedUserIndex]
}
})