【发布时间】:2021-11-13 14:01:17
【问题描述】:
我正在尝试使用 match.params.id 从 url 获取产品 ID 以传递给 api 调用,但它给出错误,匹配返回未定义是使用 match?.params?.id
Cannot read property 'params' of undefined
App.js
<Router>
<Switch>
<Route path={process.env.PUBLIC_URL +"/images/upload/:id"}>
<UploadImages />
</Route>
</Switch>
</Router>
重定向:
//params.id from material-ui datagrid table
<LinkContainer to={`/admin-panel/home/images/upload/${params.id}`}>
<button className="btn btn-primary mr-2">Set Images</button>
</LinkContainer>
const columns = [
{ field: '_id', headerName: 'ID', width: 250 },
{ field: 'name', headerName: 'Product Name', width: 300 },
{ field: 'price', headerName: 'Price', width: 130 },
{ field: 'countInStock', headerName: 'Count In Stock', width: 230 },
{ field: 'createdAt', headerName: 'Date Created', width: 230 },
{
field: 'action',
headerName: 'Action',
width: 350,
renderCell: (params)=>{
return (
<div>
<LinkContainer to={`/admin-panel/home/product/${params.id}/edit`}>
<button className="btn btn-info mr-2">Edit</button>
</LinkContainer>
<LinkContainer to={`/images/upload/${params.id}`}>
<button className="btn btn-primary mr-2">Set Images</button>
</LinkContainer>
<Button className="btn btn-danger" onClick={()=>deleteHandler(params.id)} ><Delete /></Button>
</div>
)
}
},
];
return (
<div className="productList" style={{height:600}} >
{loading ? (<Loader />) : error ? (<Message variant="danger" >{error}</Message>):(
<DataGrid
rows={products}
columns={columns}
pageSize={10}
rowsPerPageOptions={[5]}
checkboxSelection
/>
)}
</div>
)
}
上传图片.js
function UploadImages({ location, match, history }) {
const productID = match.params.id
return (<div></div>);
};
export default UploadImages
Id 在 URL 中传递
http://localhost:3000/images/upload/61555780c208142757be70v1
【问题讨论】:
标签: javascript html reactjs react-router