Чтобы использовать Link, вы должны определить свои маршруты. здесь вы можете создавать свои маршруты. Пример: я создал файл App. js для определения маршрутов
// app.js
import React, { Component } from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import Demo from './demo';
const LoginForm = () => <></>;
const SearchComponent = () => <></>;
class App extends Component {
render() {
return (
<React.Fragment>
<BrowserRouter>
<div>
<Route path="/search" component={SearchComponent} />
<Route path="/loginform" component={LoginForm} />
<Route path="/" component={Demo}/>
</div>
</BrowserRouter>
</React.Fragment>
);
}
}
export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
может передавать атрибуты ссылки кнопки как объект
// demo.js
const tableBodies = [
`order.owner.user.first_name`,
`order.owner.user.last_name`,
{
base: '/user',
icon: <VisibilityIcon />
}
]
Я определил функцию, возвращающую настраиваемую ссылку кнопки ButtonLink (), можете ли вы передать реквизиты после ее вызова
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableContainer from "@material-ui/core/TableContainer";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import { Link } from 'react-router-dom'
import Button from '@material-ui/core/Button';
import VisibilityIcon from '@material-ui/icons/Visibility';
const useStyles = makeStyles({
root: {
width: "100%"
},
container: {
maxHeight: 440
}
});
export default function StickyHeadTable({ data, tableHeaders, tableBodies }) {
const classes = useStyles();
const getProperty = (obj, prop) => {
var parts = prop.split('.');
if (Array.isArray(parts)) {
var last = parts.pop(),
l = parts.length,
i = 1,
current = parts[0];
while((obj = obj[current]) && i < l) {
current = parts[i];
i++;
}
if(obj) {
return obj[last];
}
} else {
throw 'parts is not valid array';
}
}
const ButtonLink = (prop) => {
return (
<Button
component={Link}
to={prop.link}
variant="contained"
type="button"
size="small"
className={"button-classes"}
startIcon={prop.icon}
/>
)
}
//<ButtonLink link="/mani"/>
return (
<Paper className={classes.root}>
<TableContainer className={classes.container}>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
{tableHeaders.map((header, index) => (
<TableCell key={index}>{header}</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{data.map(data => (
<TableRow key={data.id}>
{tableBodies.map(body => (
(typeof body === "string"
? <TableCell key={body}>{getProperty(data, body)}</TableCell>
: <TableCell key={body}><ButtonLink
link={body.base}
icon={body.icon}
/></TableCell>
)
))}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</Paper>
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import Demo from './demo';
import App from './app';
//ReactDOM.render(<Demo />, document.querySelector('#root'));
ReactDOM.render(<App />, document.querySelector('#root'));