【问题标题】:How to add <Link> react-router per each Material-UI TableRow?如何为每个 Material-UI TableRow 添加 <Link> react-router?
【发布时间】:2018-11-14 09:45:58
【问题描述】:

如何在 .map 中为每个 TableRow 添加链接?

*我的错误是 validateDOMNesting(...): cannot appear as a child of "" 我正在使用反应路由器 react-router-dom

如何在Table TableRow的.map的每个循环中添加链接?

   import React, { Fragment } from 'react'
import { Paper } from 'material-ui'
import Table from 'material-ui/Table';
import TableBody from 'material-ui/Table/TableBody';
import TableCell from 'material-ui/Table/TableCell';
import TableHead from 'material-ui/Table/TableHead';
import TableRow from 'material-ui/Table/TableRow';
import { connect } from 'react-redux'
// import { Edit, Delete } from '@material-ui/icons'
import { withStyles } from 'material-ui/styles'
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
const drawerWidth = 100;
class Listofbond extends React.Component {
    render() {
        console.log(this.props);
        const { MainProps, classes } = this.props;
        return (
            <Router>
                <Paper>
                    <Table >
                        <TableHead>
                            <TableRow>
                                <TableCell>Bondame</TableCell>
                            </TableRow>
                        </TableHead>
                        <TableBody>
                            {[1, 2, 3].map(n => {
                                return (
                                    <Link to={`/bond/${n}/`} key={n}>
                                        <TableRow>
                                            <TableCell component="th" scope="row">
                                                {n}
                                            </TableCell>
                                        </TableRow>
                                    </Link>
                                );
                            })}
                        </TableBody>
                    </Table>
                </Paper>
            </Router>
        )
    }

}

export default Listofbond;

【问题讨论】:

  • 在您的链接上添加父级并尝试一下。例如:
    /bond/${n}/} key={n}> {n}
  • 仍然出错 *我的错误是 validateDOMNesting(...): cannot appear as a child of ""
  • @YamoshiWolverine 你找到解决办法了吗?

标签: reactjs redux react-router material-ui react-router-dom


【解决方案1】:

链接应用于定义了 onClick 的组件,因此将您的 TableRow 包装到这样的 div 中:

<Link to={`/bond/${n}/`} key={n}>
   <div>
     <TableRow>
       <TableCell component="th" scope="row">
         {n}
       </TableCell>
      </TableRow>
   </div>
</Link>

【讨论】:

  • 仍然出错 *my error is validateDOMNesting(...): cannot appear as a child of ""
  • 我正在使用相同的并且它正在工作,但在我的情况下,不需要使用路由器标签。如果您需要更多帮助,请告诉我。
【解决方案2】:

正确的做法是使用TableRow组件的component prop:

<TableRow component={Link} to={`/bond/${n}/`} key={n}>
...
</TableRow>

【讨论】:

【解决方案3】:

有很多解决方法,因为上面的解决方案在控制台上给出了错误validateDOMNesting(...)

  1. 在文本本身(列值)周围添加Link标签 &lt;ColumnCell&gt; ,尽管它将文本更改为更像 &lt;a&gt; 标签 .. 但你可以处理这个。通过这样做 style={{ textDecoration: 'none', color: 'black' }}

                 <TableCell key={column.id} align={column.align}>
                      <Link
                        to={`/elsewhere/${row.id}`}
                        style={{ textDecoration: 'none', color: 'black' }}
                      >
                        {value}
                      </Link>
                 </TableCell>
    
  2. 为每个表格行创建一个按钮。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签