【发布时间】:2019-11-05 00:45:15
【问题描述】:
我使用的material-table库是一个基于Material-UI Table的Datatable。
数据表具有可编辑的属性,并在同一个表中添加一条新记录,但我不想在同一个表中这样做。
我需要使用一个按钮,因为我希望它打开一个材质 UI 菜单。但是,我不知道如何添加链接或类似内容。
我使用以下代码 sn-p 进行了尝试,但它告诉我 Link 没有定义,即使它确实很重要
import React, { useState, useEffect } from 'react';
import MaterialTable from 'material-table';
import EditIcon from '@material-ui/icons/Edit';
import { IconButton } from '@material-ui/core';
import axios from 'axios';
import { Link} from 'react-router-dom';
export default function TableProducts() {
const url='/api/products';
const [product, setProduct]= useState({Products:[]});
useEffect(()=>{
const getProduct=async()=>{
const response =await axios.get(url);
setProduct(response.data);
}
getProduct();
},[]);
return (
<MaterialTable
title="Products"
columns={[
{title: 'id',field: 'id', type: 'numeric', hidden:'false'},
{ title: 'nameproduct',field: 'nameproduct', },
{ title: 'description', field: 'description' },
{ title: 'price', field: 'price' },
]}
data={product.Products}
options={{
filtering: true,
sorting: true
}}
actions={[
{
icon: 'edit',
tooltip: 'Edit ',
onClick: () =>
<Link to={`/product/${data._id}/edit`}>Edit</Link>
}
]}
/>
);
}
我想完成这样的事情
<Link
to={`/product/edit/${product.id}`}
className="btn btn-success mr-2">Editar
</Link>
【问题讨论】:
-
你的代码有点欠缺。很难判断您是想在每个表格行上创建一个新链接,还是什么。
-
您确定要导入链接吗?
-
@Rob 是的,我进行了导入,从 'react-router-dom' 导入 {NavLink, Link};