reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Error with composed selector

Open stevenmusumeche opened this issue 10 years ago • 35 comments

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.

stevenmusumeche avatar Sep 10 '16 21:09 stevenmusumeche

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.

ellbee avatar Sep 11 '16 12:09 ellbee

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?

stevenmusumeche avatar Sep 11 '16 16:09 stevenmusumeche

No, getAllLocations itself is undefined according to the error. Try logging console.log(getAllLocations) just before the createSelector code to see.

ellbee avatar Sep 11 '16 16:09 ellbee

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

stevenmusumeche avatar Sep 11 '16 23:09 stevenmusumeche

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?

skizzo avatar Jan 02 '17 13:01 skizzo

I think you meant to mention @stevemusumeche.

steveluscher avatar Jan 02 '17 22:01 steveluscher

That's, @stevenmusumeche.

steveluscher avatar Jan 02 '17 22:01 steveluscher

@skizzo indeed I had to co-locate them in the same file.

stevenmusumeche avatar Jan 02 '17 23:01 stevenmusumeche

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.

chrisfisher avatar Jan 24 '17 03:01 chrisfisher

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.

digitalmaster avatar Apr 02 '17 06:04 digitalmaster

+1

BenMGilman avatar May 15 '17 21:05 BenMGilman

+1

jordan-enev avatar Jun 21 '17 14:06 jordan-enev

+1

phdog avatar Jun 27 '17 15:06 phdog

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

jimbol avatar Jun 27 '17 16:06 jimbol

+1

BarthesSimpson avatar Jun 30 '17 22:06 BarthesSimpson

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.

dwilt avatar Sep 05 '17 09:09 dwilt

In my case, I had declared createSelector before selectors functions (in one file). Propably, in your case, getAllLocations, somehow undefined yet.

Lazarencjusz avatar Feb 01 '18 07:02 Lazarencjusz

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)...

jameslaneconkling avatar Apr 16 '18 17:04 jameslaneconkling

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 avatar May 03 '18 11:05 daniula

@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!

jameslaneconkling avatar May 03 '18 14:05 jameslaneconkling