Yam*_*ine 5 reactjs react-router redux material-ui react-router-dom
如何在.map中为每个TableRow添加链接?
*我的错误是validateDOMNesting(...):使用react router react-router-dom无法显示为“ <a>” im的子级
如何在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;
Run Code Online (Sandbox Code Playgroud)
CYM*_*YMA 11
正确的做法是使用组件的componentprop TableRow:
<TableRow component={Link} to={`/bond/${n}/`} key={n}>
...
</TableRow>
Run Code Online (Sandbox Code Playgroud)
小智 8
用于component='div'几乎所有组件(Table、TableHead、TableBody、TableCells 甚至TableRow中的(仅) TableHead)以及component={Link}其他TableRows( 中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>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你会怀念 HTML Native <table>。但一切看起来都是原生的。
不幸的是,我认为没有其他办法。请参阅:/sf/answers/1200351351/
对于那些使用react-router-dom v6+的人来说,这有效:
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>
);
};
Run Code Online (Sandbox Code Playgroud)
小智 0
Link 应用于定义了 onClick 的组件,因此将 TableRow 包装到 div 中,如下所示:
<Link to={`/bond/${n}/`} key={n}>
<div>
<TableRow>
<TableCell component="th" scope="row">
{n}
</TableCell>
</TableRow>
</div>
</Link>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2015 次 |
| 最近记录: |