Skip to content

Style Guide: Ducks or Rails Style? #3639

Closed
@ajones513

Description

@ajones513

What docs page needs to be fixed?

What is the problem?

The Style Guide 'strongly recommends' Structure Files as Feature Folders or Ducks; this might not be desired in some projects, and may discourage sticking to other recommendations.

Action creators and reducers colocated

While Ducks does provide colocation of action creators and reducers, if my understanding is correct, this works against the 1:N nature of actions to the reducers that handle them. The style guide 'strongly recommends' Allow Many Reducers to Respond to the Same Action - by structuring action creators and reducers together using Ducks, this seems to be impeded.

As stated in https://redux.js.org/faq/actions:

Some users do choose to bind them more tightly together, such as the “ducks” file structure, but there is definitely no one-to-one mapping by default, and you should break out of such a paradigm any time you feel you want to handle an action in many reducers.

I'd argue that a codebase that sometimes uses Ducks and sometimes doesn't is less maintainable and less understandable than a codebase that uses a single structure consistently. Using Ducks, for a new reducer to start responding to an existing action, this would require a "break out of the paradigm" refactor. My fear here would be that those who don't realise they need to go to the effort of making the refactor just dispatch another action to target its corresponding reducer, going against Avoid Dispatching Many Actions Sequentially.

Components colocated with actions creators and reducers

In addition to structuring action creators and reducers together, the Style Guide also shows components (Todos.tsx) structured alongside the action creators and reducers (todosSlice.ts). I've always thought of the 'tree of Redux state' as independent to the 'tree of React components' - being able to use a single version of truth piece of state from Redux across many components in the application is where I've extracted lots of value from Redux.

If many different components want to use a single version of truth piece of state then the tendency would be for more and more code to migrate to common. This feels like abuse of common, and perhaps a smell that Ducks is not the optimal structure for some projects.

What should be changed to fix the problem?

If my reasoning is not flawed, state at least that a deliberate choice must be made between Ducks style or rails-style (see https://redux.js.org/faq/code-structure), and outline pros/cons of each. My own opinion (at present) would be to advise that if unsure, prefer the rails-style.

I'd be happy to create a pull request with proposed edits if this is deemed a valid direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions