react-table:根据行属性值更改行的背景颜色

问题描述

我有一种情况,我必须根据行道具更改行的背景色。我目前正在这样做:

const getTrProps = (state,rowInfo,instance) => {
        if (rowInfo) {
            return {
                style: {
                    'background-color': rowInfo.original.customercomplaints.order_ref === currentOrderId ? '' : 'yellow',}
            }
        }
        return {};
    }

react-table中,是这样的:

<ReactTableFixedColumns className="-striped"
      columns={customerComplaintsColumns}
      data={customerComplaintsData}
      daultPageSize={10}
      defaultFilterMethod={filterCaseInsensitive}
      getTrProps={getTrProps}
      />

如果我检查元素,我会得到:

<div class="rt-tr -even" role="row" style="background-color: yellow;">

,但不适用于该行。我该如何解决

我的react版本是^16.13.1,而react-table版本是6.8.6

解决方法

首先,您在分配样式方面有误。您可能希望将background-color作为对象样式。

例如

        return {
            style: {
                'background': rowInfo.original.customercomplaints.order_ref === currentOrderId ? '' : 'yellow',}
        }

此外,您可能希望不使用空字符串作为默认值,而希望使用“白色”或颜色作为默认值。

我认为这是最主要的原因,如果它不起作用,请告诉我。

由反应表维护者编写的示例链接 https://codesandbox.io/s/k3q43jpl47