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

Nick Raphael @nickraphael

About: Brought to you by Nick Raphael. Fullstack azure architect and tech lead. Currently neck deep in dotnet core - azure stack - microservices.

Location:
Sydney, Australia
Joined:
Oct 21, 2019

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

Publish Date: Jun 21 '22
8 1

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.

Comments 1 total

  • Alain D'Ettorre
    Alain D'EttorreJun 25, 2022

    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

Add comment