How to add <Link> react-router per each Material-UI TableRow?

你离开我真会死。 提交于 2020-02-03 05:59:45

问题


How to add link per TableRow in .map?

*my error is validateDOMNesting(...): cannot appear as a child of "< a >" im using react router react-router-dom

how to add link in every loop of .map in Table TableRow?

   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;

回答1:


The correct way to do this is to use the component prop of the TableRow component:

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




回答2:


Link is applied to the component for which onClick is defined, so wrap your TableRow into a div like this:

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


来源:https://stackoverflow.com/questions/50691049/how-to-add-link-react-router-per-each-material-ui-tablerow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!