Error with composed selector
I'm creating a composed selector with a simple selector from another file and I get this error.
Uncaught Error: Selector creators expect all input-selectors to be functions, instead received the following types: [undefined]
The selector I am importing from locationsSelectors.js is:
export const getAllLocations = state => state.locations;
And here is how I am trying to use it:
import {createSelector} from 'reselect';
import {getAllLocations} from './locationSelectors';
export const myNewSelector = createSelector(
[getAllLocations],
(locations) => {
return 'foo';
}
);
This is a simplified example to demonstrate the problem. The weird thing is that if I move the imported getAllLocations selector into this file, it works fine. Another weird thing is that I can import selectors for composition from other files as well, but only some of them.
The error is saying that getAllLocations is undefined, so I guess something is wrong with the export from locationSelectors. I'm going to close this as it doesn't appear to be a Reselect issue, but if you give me a link to some example code that I can run that demonstrates the problem I'll see if I can figure whats happening.
That was my first guess too, however, the selectors in locationSelectors are used throughout the app with no problem. There are some situations where the RESULT of getAllLocations could be undefined, would that matter?
No, getAllLocations itself is undefined according to the error. Try logging console.log(getAllLocations) just before the createSelector code to see.
As it turns out, this is a webpack/ES6 module circular dependency issue. The value of getAllLocations eventually resolves correctly, but it is undefined at first which causes reselect to throw the error. Outside of moving the shared code, is there a way to address this in reselect? Details here: http://stackoverflow.com/questions/35240716/webpack-import-returns-undefined-depending-on-the-order-of-imports
I'm running into the same problem that reselect basically only works properly if all the selectors are in one file.. @steveluscher did you manage to import selectors from other files properly?
I think you meant to mention @stevemusumeche.
That's, @stevenmusumeche.
@skizzo indeed I had to co-locate them in the same file.
A workaround which worked for me was to define the imported selector using standard function syntax, e.g. change:
export const getIsLoading = state => state.isLoading
to:
export function getIsLoading(state) { return state.isLoading }
But this is annoying as arrow syntax is more ideal.
Just spend all day dealing with this too!! Ugh!. Wish there was a warning about this in the readme.. since it's obvious that unless all your selectors are in the same file (they shouldn't be) you will run into this issue when composing selectors.
+1
+1
+1
Super frustrating. We've been battling with this for a while. This is often related to circular dependencies. When we run into this issue we use madge to detect circular dependencies.
When there's a circ dependency, we have to evaluate what is causing it. Usually, the fix involves moving a couple selectors into another file where they make more sense. Otherwise, we pull them into a core folder. Also all of our top level selectors (like const getFoos = (state) => state.foos) live in this global folder.
This issue isn't specific to reselect. Any library that composes many dependencies at initialize-time will have similar problems. As @stevenmusumeche indicated
this is a webpack/ES6 module circular dependency issue
+1
this is a webpack/ES6 module circular dependency issue
This is not a Webpack or a Reselect problem at all. Your code is not de-coupled correctly. If you have issues where code is ultimately depending on itself through a circular dependency, you need to look at breaking apart your code into separate files/responsibilities.
That being said, there are times when I have specifically UI-driven selectors depending on purely data-driven selectors but you have to be extremely careful when doing this and understand your structure very well. And even then, I'm questioning whether it's the right approach.
In my case, I had declared createSelector before selectors functions (in one file). Propably, in your case, getAllLocations, somehow undefined yet.
Circular dependencies between selectors are not necessarily a code architecture issue. Consider the following contrived blog-ish example:
// posts.js
import getCurrentUser from './users';
export const getPosts = (state) => state.posts;
/**
* @param {Object} state
*/
export const getPostsByCurrentUser = createSelector(
getPosts,
getCurrentUser,
(posts, user) => posts.filter(({ userId }) => userId === user.id)
);
// users.js
import getPosts from './posts';
export const getCurrentUser = (state) => state.users.current;
/**
* @param {Object} state
* @param {String} userId
*/
export const getUserPostCount = createSelector(
getPosts,
(_, userId) => userId
(posts, userId) => posts.filter(({ userId }) => userId === user.id).length
);
There is nothing explicitly wrong w/ having a circular dependency between posts.js and users.js. AFAIK, the circular dependency === bad logic applies mostly to OOP situations, e.g. circular dependencies bt/ classes. But in functional situations in general, circular dependencies between functions aren't really an anti pattern (it's required for mutual recursion), and I don't think the specific case of circular dependencies across modules would necessarily be any different.
Given that a selector might not be available at module evaluation (when the selector is created) should not matter, given that the selector is not invoked until later, by which point the reference should have resolved. For this reason, circular dependencies bt/ functions are generally not an issue, assuming that the functions are not invoked before all modules have been evaluated.
However, b/c webpack (and afaik all other bundlers) passes arguments to createSelector as values, not as bindings, they do not end up pointing to the correct selector function after the dependent module is evaluated, and are still undefined when the selector is invoked.
es6 module bindings are supposed to handle this case correctly, but afaik, no bundler build tool gets this case right. So, :shrug-emoji, until one does, I guess the only workaround is to ensure that selectors don't contain circular dependencies (even if they arguably should be allowed to do so)...
I stumble over this issue and immediately came up with simple solution (based on @jameslaneconkling example):
// posts.js
export const getPostsByCurrentUser = createSelector(
getPosts,
(state, props) => getCurrentUser(state, props),
(posts, user) => posts.filter(({ userId }) => userId === user.id)
);
export const getUserPostCount = createSelector(
(state, props) => getPosts(state, props),
(_, userId) => userId
(posts, userId) => posts.filter(({ userId }) => userId === user.id).length
);
Maybe it will be helpful for somebody if they really don't want to start reorganising their whole project structure. Does anybody see downsides of this approach?
@daniula wow, yeah, of course. simply delay the resolution of the selector (getPosts, in the above case) until the function is called, rather than when the module is first evaluated. Good thinking--thanks!