ESLint:组件定义缺少 displayName (react/display-name)

Leo*_*mer 25 javascript reactjs antd react-hooks

我正在使用带有 antd 的 react hook 组件。为表设置列时,渲染函数给我一个 ESLint 错误:

ESLint:组件定义缺少 displayName (react/display-name)

我试过将 displayName 添加到对象中,但这不起作用。

这是错误的样子: 在此处输入图片说明

这是代码:

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

任何人都可以帮忙吗?

这是完整的组件代码(只是相关位)

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)
Run Code Online (Sandbox Code Playgroud)

Aro*_*sha 39

如果有人需要在所有文件中避免这种情况,请将以下内容添加到.eslintrc.js文件的规则部分,

{
  ...
  "rules": {
    "react/display-name": "off"
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 37

ESLint 认为您正在定义一个新组件,而没有为其设置任何名称。

这是因为 ESLint 无法识别渲染道具模式,因为您不是直接将此渲染道具写入组件,而是写入对象。

您可以将renderprop 直接放入<Column>组件的jsx 实现中,或者通过执行以下操作来关闭 ESLint 的错误:

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>
        ),
    }
]
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助;)

  • 我已经知道如何关闭 eslint 错误;-) 请您扩展答案的“您可以将渲染道具直接放入 &lt;Column&gt; 组件的 jsx 实现中”部分。 (6认同)

kag*_*ajm 23

render键使用普通函数也将删除 ESLint 警告,而无需禁用警告。

const columns_payment_summary_table = [ 
    {
        title: SettlementConstants.LABEL_QUANTITY_SELECTED,
        dataIndex: 'group',
        key: 'group',
        render: function countForCountry(text) {
            return <span>{getCountForCountry(text)}</span>
        },
    }
]
Run Code Online (Sandbox Code Playgroud)


ava*_*tar 6

有时,如果我们只有一两个地方有错误,我们可以绕过规则。如果我们在多个地方有相同的用例怎么办。每次我们都必须禁用规则。

相反,我们可以通过将函数分配给渲染属性来绕过这个错误。

const getMyHTML = (text) => <span>{getCountForCountry(text)}</span>

const columns_payment_summary_table = [
  {
    title: SettlementConstants.LABEL_QUANTITY_SELECTED,
    dataIndex: 'group',
    key: 'group',
    render: getMyHTML,
  }
]
Run Code Online (Sandbox Code Playgroud)