dzintars / front

Test setup (Don't use!) Dev server URL available while i'm developing.
https://front.oswee.dev
1 stars 1 forks source link

How to namespace selectors? #118

Open dzintars opened 4 years ago

dzintars commented 4 years ago

I am looking for a patters how to correctly export selectors under some namespace. All examples i find online does not group/namespace selectors and i am not sure why is so. But is found that importing separate selectors looks kinda dirty. One way would be to export selectors as object properties like in this example:

// INPUT/BASE SELECTORS
const getState = (state: RootState): AppNavigationState => state.appModules

// MEMOIZED SELECTORS
const selectFetchState = createSelector([getState], state => state.fetching)

export const AppNavigationSelectors = {
  selectFetchState,
}

and then to use them as AppNavigationSelectors.selectFetchState(state). If in some component many selectors are used, then this could tidy things up.

BUT i still have some doubts... probably my overall code organization is wrong...

DzintarsToday at 12:56 PM Is there some significant reason i don't see

export const FeatureSelectors = {
  selectFetchState,
  selectSomeEntitiesArray,
  selectCurrentEntityId,
}

used a lot in repositories? I mean, some kind of selector namespacing. As i understand, any given component typically would not use many many selectors and thus, there is no big need for namespacing them, but i'm still curious about some other reasons i am not aware of.

phryneasToday at 12:57 PM Well, you're creating exports on top of exports there. You've gained nothing, assuming that the file itself has one purpose and is named well. And you lost the ability to import only one thing.

DzintarsToday at 1:00 PM You picked a right wording. I am still doubting, should i namespace them or not and... i have no clear answer why would i do so. :slight_smile:

phryneasToday at 1:00 PM Well, your file is already your namespace.

DzintarsToday at 1:00 PM thats true

phryneasToday at 1:00 PM If your file is so big you need namespaces, it should probably be a folder I should get a mug with that print :smile:

DzintarsToday at 1:06 PM Thank you. Ok... i think i got an idea... My file is not that big... I think the only reason why i "wanted" to namespace them was when i was using different features selectors in single component. In this case it "looks" cleaner MyNamespaceX.selectSomething(state) and MyNamespaceY.selectSomethingSimilaryNamed(state)... It's not a problem for me at all ... most likely it's premature "optimizations" i am trying to kill my time on. :slight_smile: Just researching

phryneasToday at 1:07 PM Sounds like you have too generic variable names ^^ You can still rename these at import if they're not clear :wink:

DzintarsToday at 1:08 PM Yeah.. i know that. Ok, thank you a lot... you just solved some of my doubts :slight_smile: