'Button onClick in MUI Datagrid invoked without clicking
I implemented a button inside the MUI v5 Datagrid. Based on the official sample https://mui.com/x/react-data-grid/columns/#render-cell , I added the onClick in the Button.
The onClick function was called when the cell was rendered for the first time instead of clicking event.
const downloadSearchResult = (params) => {
const link = document.createElement("a");
link.download = "download.txt"
link.href = "./download.txt";
link.click();
}
const notes = [
{id: 1, Search_result: "download.txt" },
{id: 2, Search_result: "pending" },
{id: 3, Search_result: "pending" },
]
export default function Notes() {
var show
const columns: GridColDef [] = [
{ field: 'Search_result', headerName: 'Search result', width: 150,
renderCell: (params) => (
<strong>
{show = (params.value == "Pending")}
{show && params.value}
{{!show &&
<Button
variant="contained" size="small"
onClick={downloadSearchResult(params)}
>
Download
</Button>}}
</strong>
)
},
];
return (
<Container>
<div>
<DataGrid rows={notes} columns={columns} />
</div>
</Container>
)
}
onClick function in Call Stack:
Button in MUI v5 Datagrid:
Solution 1:[1]
try
<Button
variant="contained" size="small"
onClick={()=>downloadSearchResult(params)}
>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Aymen |