ESLint prefers-arrow-callback on array.map

import React from 'react';

export default class UIColours extends React.Component {
  displayName = 'UIColours'

  render() {
    const colours = ['black', 'red', 'white', 'orange', 'green', 'yellow', 'blue', 'darkblue', 'lilac', 'purple', 'darkPurple'];
    return (
      <div className="ui-colours-container row bg-white">
        <div className="col-md-16 col-xs-16 light">
          <div className="color-swatches">
            {colours.map(function(colour, key) {
              return (
                <div key={key} className={'strong color-swatch bg-' + colour}>
                  <p>{colour}</p>
                </div>
              );
            })}
          </div>
        </div>
      </div>
   );
  }
}

12:26 error Unexpected function expression - callback arrow

I looked at the map documentation and cannot find a good example of several parameters.

+4
source share
2 answers

This ESLint rule happens because you have an anonymous function as a callback, so it assumes that you are using an arrow function instead. To use several parameters with arrow functions, you need to wrap the parameters in brackets, for example:

someArray.map(function(value, index) {
  // do something
});

someArray.map((value, index) => {
  // do something
});

, MDN , .

ESLint , . ESLint prefer-arrow-callback.

+14

map :

{colours.map(colour => (
  <div key={`colour-${colour}`} className={`strong color-swatch bg-${colour}`}>
    <p>{colour}</p>
  </div>
)}

, key .

+1

All Articles