Artem Vertiy
Artem Vertiy

Reputation: 1118

Object Literal Shorthand Syntax. Is trailing comma required?

I'm going through react-redux boilerplate

there is a code there

function mapDispatchToProps(dispatch) {
  return {
    dispatch,
  };
}

I wondered why a comma after dispatch is required? i.e. when i remove comma eslint hints an error:

Insert ',' eslint(prettier/prettier)

I played it with babeljs.io either with/without comma the code is converted into

function mapDispatchToProps(dispatch) {
  return {
    dispatch: dispatch
  };
}

It does not matter a lot but to understand why eslint forces me to put the comma..

Upvotes: 0

Views: 94

Answers (1)

Quentin
Quentin

Reputation: 943142

See the ESLint documentation:

Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched. Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array:

Upvotes: 2

Related Questions