DEV Community

Cover image for Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'mySlice')
Nick Raphael
Nick Raphael

Posted on

Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'mySlice')

Oh man, I just had a really annoying redux-toolkit error...

Uncaught TypeError: Cannot read properties of undefined (reading 'mySlice')
Enter fullscreen mode Exit fullscreen mode

This was caused by a circular dependency in my file structure. I have a file structure like this...

stuff
  -> store
    -> index.ts
       reducer.ts
       selectors.ts
  feature
    -> store
      -> index.ts
         slice.ts
         selectors.ts
Enter fullscreen mode Exit fullscreen mode

Any code outside of the store files references store stuff via the index.tx which just exports all the other files.

My problem was a selector referencing a selector from a higher level in my store structure via another index.ts. The reducer at this higher level was referencing the slice at the lower level, again via index.ts.
This is a circular reference. To fix it, I chose to remove the higher level index.ts and reference the selector file directly.

Hope this helps somebody.

Latest comments (1)

Collapse
 
alaindet profile image
Alain D'Ettorre

I know the feeling, but I still love index.ts barrel files. My approach is to always use relative imports inside a module and to use aliases (with index.ts) only when importing from outside. A module for me is a folder containing something independent enough from the rest, not just any folder. This way you can safely remove some index.ts files without messing with the imports