从反应表中的按钮打开MUI模态不起作用
我有一个反应表,其中包括每行包含一个按钮的列之一,应使用MUI的模态查看行详细信息。
这是我的列数据的片段:
export const COLUMNS = [
{...},
{
Header: 'Actions',
Cell: (row) => (
<span>
{row.row.original.id !== null ? (
<>
<button value={row.row.original.id} onClick={(e) => handleModal(e, row.row.original.id)}>
<FontAwesomeIcon icon={someIcon} />
</button>
</>
) : "No ID!"}
</span>
),
className: 'actions'
}
];
在另一个文件中,我有此handlemodal
函数:
export function handleModal(e, row) {
const [open, setOpen] = useState(false);
setOpen(true);
console.log('Row details: ', row);
return (
<BasicModal open={open} onClose={() => setOpen(false)} row={row}/>
)
}
然后,basic modal
component在一个单独的文件中:
export default function BasicModal(props) {
// const {row, open, handleClose} = props;
// const [open, setOpen] = React.useState(true);
// const handleOpen = () => setOpen(true);
// const handleClose = () => setOpen(false);
console.log("PROPS in MODAL", props);
return (
<div>
{/* <Button onClick={handleOpen}>Open modal</Button> */}
<Modal
open={props.open}
onClose={props.onClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style}>
<Typography id="modal-modal-title" variant="h6" component="h2">
Text in a modal
</Typography>
<Typography id="modal-modal-description" sx={{ mt: 2 }}>
Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
</Typography>
</Box>
</Modal>
</div>
);
}
我不确定为什么模态不显示任何内容以及如何制作它工作,我还继续获得此缩小react错误
说:
Invalid hook call. Hooks can only be called inside of the body of a function component.
This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
I have a react-table with one of the columns containing buttons for each row which should be used to view the row details using a modal from MUI.
Here is the snippet of my column data for the table:
export const COLUMNS = [
{...},
{
Header: 'Actions',
Cell: (row) => (
<span>
{row.row.original.id !== null ? (
<>
<button value={row.row.original.id} onClick={(e) => handleModal(e, row.row.original.id)}>
<FontAwesomeIcon icon={someIcon} />
</button>
</>
) : "No ID!"}
</span>
),
className: 'actions'
}
];
In another file, I have this handleModal
function:
export function handleModal(e, row) {
const [open, setOpen] = useState(false);
setOpen(true);
console.log('Row details: ', row);
return (
<BasicModal open={open} onClose={() => setOpen(false)} row={row}/>
)
}
Then the BasicModal
component in a separate file:
export default function BasicModal(props) {
// const {row, open, handleClose} = props;
// const [open, setOpen] = React.useState(true);
// const handleOpen = () => setOpen(true);
// const handleClose = () => setOpen(false);
console.log("PROPS in MODAL", props);
return (
<div>
{/* <Button onClick={handleOpen}>Open modal</Button> */}
<Modal
open={props.open}
onClose={props.onClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style}>
<Typography id="modal-modal-title" variant="h6" component="h2">
Text in a modal
</Typography>
<Typography id="modal-modal-description" sx={{ mt: 2 }}>
Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
</Typography>
</Box>
</Modal>
</div>
);
}
I not sure why the modal is not showing anything and how to make it work and I also keep getting this Minified React error
saying:
Invalid hook call. Hooks can only be called inside of the body of a function component.
This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我愚蠢的是不要彻底阅读提到的缩小错误,正如文档所说的
So it was dumb of me not to read thoroughly the minified error mentioned and as the documentation said,
So what I did instead is to create a modal function and called it instead of a button.
];