admin管理员组文章数量:1388172
When trying to send the selected rows of a Material-UI data-grid ponent to a React Hook, the site locks up with the following error:
Warning: Maximum update depth exceeded. This can happen when a ponent calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
I may be going about it the wrong way, but what I'm hoping to do is pass the data of the currently selected rows into a React Hook so that I can action it.
import React, { useState } from "react";
import { DataGrid } from "@material-ui/data-grid";
const IndexPage = () => {
const [test, setTest] = useState([]);
function currentlySelected(selections) {
setTest(selections);
console.log(test);
}
const rows = [
{ id: 1, name: "Example 1", price: "$10.99" },
{ id: 2, name: "Example 2", price: "$12.50" }
];
const columns = [
{ field: "name", headerName: "Name", width: 300 },
{ field: "price", headerName: "Price" }
];
const sortModel = [
{
field: "name",
sort: "asc"
}
];
return (
<>
<div style={{ height: "50vh" }}>
<DataGrid
sortingOrder={["desc", "asc"]}
sortModel={sortModel}
rows={rows}
columns={columns}
pageSize={100}
rowHeight={38}
checkboxSelection
onSelectionChange={newSelection => {
console.log(newSelection.rows)
// **** The following line breaks the page upon selection ****
// currentlySelected(newSelection)
}}
/>
</div>
</>
);
};
export default IndexPage;
Inside of the DataGrid
you'll notice an onSelectionChange
property with some mented out code. Unmenting that line, allowing for the onSelectionChange
to update the test
React Hook is what causes the page to break.
What is the proper way of pushing the currently selected fields up to a hook?
Code Sandbox Example Link.
When trying to send the selected rows of a Material-UI data-grid ponent to a React Hook, the site locks up with the following error:
Warning: Maximum update depth exceeded. This can happen when a ponent calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
I may be going about it the wrong way, but what I'm hoping to do is pass the data of the currently selected rows into a React Hook so that I can action it.
import React, { useState } from "react";
import { DataGrid } from "@material-ui/data-grid";
const IndexPage = () => {
const [test, setTest] = useState([]);
function currentlySelected(selections) {
setTest(selections);
console.log(test);
}
const rows = [
{ id: 1, name: "Example 1", price: "$10.99" },
{ id: 2, name: "Example 2", price: "$12.50" }
];
const columns = [
{ field: "name", headerName: "Name", width: 300 },
{ field: "price", headerName: "Price" }
];
const sortModel = [
{
field: "name",
sort: "asc"
}
];
return (
<>
<div style={{ height: "50vh" }}>
<DataGrid
sortingOrder={["desc", "asc"]}
sortModel={sortModel}
rows={rows}
columns={columns}
pageSize={100}
rowHeight={38}
checkboxSelection
onSelectionChange={newSelection => {
console.log(newSelection.rows)
// **** The following line breaks the page upon selection ****
// currentlySelected(newSelection)
}}
/>
</div>
</>
);
};
export default IndexPage;
Inside of the DataGrid
you'll notice an onSelectionChange
property with some mented out code. Unmenting that line, allowing for the onSelectionChange
to update the test
React Hook is what causes the page to break.
What is the proper way of pushing the currently selected fields up to a hook?
Code Sandbox Example Link.
Share Improve this question edited Oct 26, 2020 at 20:56 ekfuhrmann asked Oct 26, 2020 at 16:17 ekfuhrmannekfuhrmann 1,7392 gold badges12 silver badges12 bronze badges1 Answer
Reset to default 4What is happening is when you render DataGrid
it is most firing onSelectionChange
. From there, you are setting your state which re-renders the grid, which calls onSelectionChange
... and you are into an infinite loop.
You can fix this by either:
- Only calling
setTest
if selections passed byonSelectionChange
does not equal what you have in state already.
const IndexPage = () => {
const [test, setTest] = useState([]);
function currentlySelected(selections) {
if (test !== selections) { // I didn't write it in but you'll need to do object parison here
setTest(selections)
}
}
...
return (
<div style={{ height: "50vh" }}>
<DataGrid
onSelectionChange={currentlySelected}
/>
</div>
)
}
- Store the selected row in a ref
const IndexPage = () => {
const test= useRef({});
function currentlySelected(selections) {
test.current = selections;
}
...
return (
<div style={{ height: "50vh" }}>
<DataGrid
onSelectionChange={currentlySelected}
/>
</div>
)
}
Option 2 may or may not make sense depending on what you want to do with the selected rows, but either of these should stop your page from locking up
本文标签: javascriptPassing MaterialUI Data Grid onSelectionChange to a react hookStack Overflow
版权声明:本文标题:javascript - Passing Material-UI Data Grid onSelectionChange to a react hook - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744567805a2613158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论