Search code examples
javascriptreactjsantdreact-hooks

ESLint: Component definition is missing displayName (react/display-name)


I'm using a react hook component with antd. When setting up columns for a table, the render function is giving me an ESLint error:

ESLint: Component definition is missing displayName (react/display-name)

I've tried adding displayName to the object but this doesn't work.

This is how the error looks: enter image description here

This is the code:

const columns_payment_summary_table = [ 
    {
      title: FooConstants.LABEL_QUANTITY_SELECTED,
      dataIndex: 'group',
      key: 'group',
      render: text => (
        <span>{getCountForCountry(text)}</span>
      ),
    }
  ]

Can anyone help?

Here is full component code (well just the relevant bits)

import * as FooConstants from './constants'
import {connect} from 'react-redux'
import React, {useState, useEffect} from 'react'
import {Card, Table} from 'antd'
import PropTypes from 'prop-types'

const propTypes = {
  foos: PropTypes.object.isRequired,
}

function Foos(props) {

  const [selectedFooRows, setSelectedFooRows] = useState([])

  useEffect(() => {
    getFooDetails()
  }, [])

  function getFooDetails() {
    props.dispatch({
      type: FooConstants.GET_FOO_PAYMENT_SUMMARIES,
      params: {
        'group_by': 'country_code',
        'type': FooConstants.CLAIM_FOO,
      }
    })
    props.dispatch({
      type: FooConstants.GET_FOO_PAYMENTS,
      params: {'type': FooConstants.CLAIM_FOO, }
    })
  }

  const columns_payment_summary_table = [
    {
      title: FooConstants.LABEL_QUANTITY_SELECTED,
      dataIndex: 'group',
      key: 'group',
      render: text => (
        <span>{getCountForCountry(text)}</span>
      ),
    }
  ]

  function getCountForCountry(country_code){
    let selected_country = selectedFooRows.filter(function(row){
      return row.group === country_code
    })

    if(selected_country && selected_country.length > 0){
      return selected_country[0].ids.length
    } else {
      return 0
    }
  }

  return (
    <div>
      <Card
        title={FooConstants.LABEL_FOO_SUMMARY}>
        <Table
          columns={columns_payment_summary_table}
          bordered={true}
          dataSource={props.foos.foo_payment_summaries}
          loading={props.foos.foo_payment_summaries_pending && !props.foos.foo_payment_summaries}
          rowKey={record => record.group}
        />
      </Card>
    </div>
  )
}

Foos.propTypes = propTypes

const mapStateToProps = (state) => {
  return {foos: state.foosReducer}
}

export default connect(
  mapStateToProps,
)(Foos)

Solution

  • ESLint thinks you are defining a new component without setting any name to it.

    This is explained because ESLint cannot recognize the render prop pattern because you are not directly writing this render prop into a component, but into an object.

    You can either put the render prop directly into your jsx implementation of the <Column> component,

     const columns_payment_summary_table_render = text => (<span>{getCountForCountry(text)}</span>);
     const columns_payment_summary_table = [{
        title: SettlementConstants.LABEL_QUANTITY_SELECTED, 
        dataIndex: "group", 
        key: "group",        
        // eslint-disable-next-line react/display-name        
        render: columns_payment_summary_table_render
     }];
    

    or shut down the ESLint's error by doing this :

    const columns_payment_summary_table = [ 
        {
            title: SettlementConstants.LABEL_QUANTITY_SELECTED,
            dataIndex: 'group',
            key: 'group',
            // eslint-disable-next-line react/display-name
            render: text => (
                <span>{getCountForCountry(text)}</span>
            ),
        }
    ]
    

    I hope it helped ;)