Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
like image 231
Yamoshi Wolverine Avatar asked Jun 05 '18 02:06

Yamoshi Wolverine


People also ask

How do I add a hyperlink in Dom react router?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.

Do I need to install both react router and react router Dom?

@snAtchnAren It's not mandatory. You should never need the "react-router" package if you've already installed "react-router-dom".


3 Answers

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>

like image 193
CYMA Avatar answered Oct 17 '22 17:10

CYMA


Use component='div' for almost all components (Table, TableHead, TableBody, TableCells and even the (only) TableRow in TableHead) and component={Link} for the other TableRows (in TableBody):

<TableContainer>
  <Table component='div'>
    <TableHead component='div'>
      <TableRow component='div'> { /* Use `div` here, too */ }
        <TableCell component='div'>
           ...
        </TabelCell>
        <TableCell component='div'>
           ...
        </TabelCell>
        ...
      </TableRow>
    </TableHead>

    <TableBody component='div'>
      <TableRow component={Link}> { /* Use `Link` here */ }
        <TableCell component='div'>
           ...
        </TabelCell>
        <TableCell component='div'>
           ...
        </TabelCell>
        ...
      </TableRow>
      <TableRow component={Link}> { /* Use `Link` here */ }
        ...
      </TableRow>
      ...
    </TableBody>
  </Table>
</TableContainer>

By the way, you miss HTML native <table>. But everything looks as native.

I think, unfortunately, there is no other way. See: https://stackoverflow.com/a/17147876/5318303

like image 4
Mahdi Cheraghi Avatar answered Oct 17 '22 19:10

Mahdi Cheraghi


For those using react-router-dom v6+, this works:

import { useNavigate } from "react-router-dom";

...
export const MyComponent({ }) {
    let navigate = useNavigate();
    // Using table as per the OP's code:
    return (
        <TableContainer>
            <Table>
                <TableHead>
                    <TableRow>
                        <TableCell>Bondame</TableCell>
                    </TableRow>
                </TableHead>
                <TableBody>
                    {[1, 2, 3].map(n => {
                        return (
                            <TableRow onClick={() => { navigate(`/bond/${n}/`); }}>
                                <TableCell component="th" scope="row">
                                    {n}
                                </TableCell>
                            </TableRow>
                        );
                    })}
                </TableBody>
            </Table>
        </TableContainer>
    );
};
like image 3
ndtreviv Avatar answered Oct 17 '22 18:10

ndtreviv