Skip to content

Tips on how to avoid circular dependencies with the module pattern and inferring RootState from rootReducer? #154

Closed
@thomasvanlankveld

Description

@thomasvanlankveld

I keep running into circular dependency issues, which mostly seem to stem from inferring RootState from the rootReducer.

What happens in many of my tests is that they import things from modules via index.ts files, which list a bunch of files. In many cases, this triggers the loading of files that depend on RootState, which needs rootReducer, which needs all the other reducers, all of which are loaded from modules via index.ts files. At this point, often one of these index files is hit for the second time and the loader halts, leaving several variables undefined and resulting in cryptic error messages in the test output. If this doesn't go wrong, there is still a possibility for things to go wrong in the dependencies of these reducers.

What is the recommended pattern here? Some approaches I can think of are:

  • Move away from the module pattern and to put all reducers (and their dependencies) in a single place.
  • Move away from RootState inference and just define it explicitly.
  • Put reducers in a separate place inside each module, and don't expose them via the module's index.ts. Do something similar with all of the reducer dependencies.

I'm currently trying out something similar to the third option. Any thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions