简体   繁体   English

语义 UI 表中的可点击行返回错误

[英]Clickable rows in Semantic UI table returns errors

For creating a table it was used Semantic UI's Table .为了创建一个表,它使用了语义 UI 的 Table It looks fine but I want to have all the rows clickable and when they are clicked to redirect to the page in the link, so I added Link from react-router-dom .看起来不错,但我希望所有行都可以点击,并且当它们被点击以重定向到链接中的页面时,所以我添加了来自 react-router-dom 的 Link

The rows are indeed clickable now but in console I get some error messages:这些行现在确实可以点击,但在控制台中我收到一些错误消息:

index.js:1 Warning: validateDOMNesting(...): <td> cannot appear as a child of <a>.
in td (created by TableCell)
in TableCell (at GenericTable.js:193)
in a (created by LinkAnchor)
in LinkAnchor (created by Context.Consumer)
in Link (created by TableRow)
in TableRow (at GenericTable.js:143)
in tbody (created by TableBody)
in TableBody (at GenericTable.js:141)
in table (created by Table)
in Table (at GenericTable.js:105)

This is the code:这是代码:

import React from 'react';
import { Table } from 'semantic-ui-react';
import { Link } from 'react-router-dom'; // used for Link

export default class GenericTable extends React.PureComponent {
  constructor(props) {
    super(props);
  }

  render() {
    const {
      headers,
      emptyFirstHeader,
      rows,
      id,
      entityName,
      idList,
    } = this.props;

    return (
      <Table id={id}>
        <Table.Header>
          <Table.Row>
            <Table.HeaderCell />
          </Table.Row>
        </Table.Header>

        <Table.Body>
          {rows.map((row, rowIndex) => (
            <Table.Row
              key={idList && idList[rowIndex]}
              as={Link} // this line makes the row clickable but also adds the errors
              to={entityName && `/${entityName}/${idList[rows.indexOf(row)]}`}> // the location of the redirect
              {row.cells.map((cell, cellIndex) => {
                if (cell === undefined) {
                  return null;
                }
                return (
                  <Table.Cell
                    key={idList && `${idList[rowIndex]} ${headers[cellIndex]}`}>
                    {cell}
                  </Table.Cell>
                );
              })}
            </Table.Row>
          ))}
        </Table.Body>
      </Table>
    );
  }
}

I noticed that it also affects some css classes which are not taken into account when this error appears.我注意到它还会影响一些在出现此错误时未考虑的 css 类。 If the line as={Link} is commented, the css works fine again but the rows aren't clickable anymore.如果注释了as={Link}行,则 css 再次正常工作,但行不再可点击。

Any suggestions about how to get rid of that error?有关如何摆脱该错误的任何建议?

You need to add an onclick function to the row to do your navigation.您需要向该行添加一个 onclick 函数来进行导航。 Something like this.像这样的东西。

constructor(props) {
  super(props);
}
  
navigateTo(path) {
  // navigate to path
}

...
<Table.Row onClick={()=>{this.navigateTo(`where to to`)}}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM