DianaSuvorova / eslint-plugin-react-redux

Enforcing best practices for react-redux

Home Page:https://www.npmjs.com/package/eslint-plugin-react-redux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Looking for a rule that forbids mapDispatchToProps

ackvf opened this issue · comments

Is it possible to configure your connect-prefer-minimum-two-arguments so that it only allows one argument that is mapStateToProps?

If not, are you, by any chance, aware whether there are other eslint plugins for react-redux that allow this? Thanks

@ackvf What is the use case? I haven't heard of the case where mapDispatchToProps shouldn't be allowed...

We use Redux Ducks with Thunks in a form of a singleton and moreover, our actions are self-contained meaning:

  • We export/import all related actions at once using a single entry point - the Duck
  • We never define special dispatch behaviour outside the actions - each action performs what is necessary or dispatches other actions in response to async events. Therefore we do not need to remap the actions using a mapDispatchToProps as it would be plain:
import { fetchUsers, signIn, signOut } from 'redux/users/actions.js'

const mapDispatchToProps = dispatch => {
  return {
    fetchUsers: () => dispatch(fetchUsers()),
    signInt: () => dispatch(signInt()),
    signOut: () => dispatch(signOut())
  }
}
// an object shorthand can be used too to simplify things

our way

import UserDuck from 'redux/UserDuck.js'

// no mapDispatchToProps
// later in the component
componentDidMount() {
  this.props.dispatch(UserDuck.fetchUsers())
}

Also notice that the first approach requires you to first enumerate all actions (okay, import * as UserActions from ... can be used. Anyway, importing the Duck as a Singleton plays well with typescript as we automatically get intellisense for all Duck properties (the actions) as well as their call signatures.