单击删除时会引发错误未知事件处理程序属性“onDelete”
2020-06-04
1477
点击主屏幕上的
删除
图标时,系统抛出以下错误,并且不会删除播放器。
Warning: Unknown event handler property `onDelete`. It will be ignored.
in div (created by ForwardRef(Modal))
in ForwardRef(Portal) (created by ForwardRef(Modal))
in ForwardRef(Modal) (created by ForwardRef(Dialog))
Home.js
const [deleteDialog, setDeleteDialog] = useState(false);
const [playerId, setPlayerId] = useState("");
const deletePlayer = (id) => e => {
setPlayerId(id);
setDeleteDialog(true);
}
const onDelete = id => () => {
try {
Axios.delete('http://localhost:8000/service/player', id);
setDeleteDialog(false);
} catch (e) {
console.log(e);
}
}
return (
<div className="App">
<div className="wrapper">
<div className="playerList_header">
<h1>Players</h1>
</div>
<div className="playerList_home_page">
<div className="grid-container">
{
searchResults.map(({ id, image, position, name }) => (
<div key={id} className="grid-item">
{
deleteIcon.show && (
<span className="deletePlayerButton" onClick={deletePlayer(id)}>
<img className="deletePlayerimg" src="/images/delete.png"></img>
</span>
)}
<div>
<img alt="" className="playerProfilePic_home_tile" key={image} src={image}></img>
</div>
<div className="playerProfile_grid_border">
<span className="rec_name_position_data">
<h3 key={name}>{name}</h3>
<span className="playerPosition_home_tile" key={position}>{position}</span>
</span>
</div>
</div>
))
}
</div>
</div>
</div>
<AlertDialog
onDelete={onDelete(playerId)}
open={deleteDialog}
onClose={() => setDeleteDialog(false)}
playerId={playerId}
/>
</div>
);
Dialog.js
在
是
按钮上
onClick={ onDelete(playerId)
export default function AlertDialog({ open, onClose, onDelete, playerId }) {
return (
<Dialog
open={open}
onClose={onClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{"Delete the player ?"}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Pressing Yes will delete the player with ID {playerId}.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={onClose} color="primary">
No
</Button>
<Button onClick={ onDelete(playerId) } color="primary" autoFocus>
Yes
</Button>
</DialogActions>
</Dialog>
);
}
Server.js
app.delete('/service/player', async (req, res) => {
try {
const userId = req.body.id;
console.log("UserId"+userId);
const deletePlayer = await UserModel.destroy({
where:{ id : userId }
})
res.status(200).json({ deletePlayer });
} catch (e) {
res.status(500).json({ fail: e.message });
}
});
2个回答
确保将
playerId
传递给
onDelete
方法
第一种方法
由于您的 onDelete 函数在函数内部有一个函数。只需调用
onDelete
并传递 playerId
<Button onClick={onDelete(playerId)} color="primary" autoFocus>
Yes
</Button>
第二种方法
正确重写
onDelete
。无需将 fun 放在 fun 中。
const onDelete = id => {
try {
Axios.delete('http://localhost:8000/service/player', id);
setDeleteDialog(false);
} catch (e) {
console.log(e);
}
}
现在只需调用为 onClick 提供一个内联箭头函数
<Button onClick={() => onDelete(playerId)} color="primary" autoFocus>
Yes
</Button>
gdh
2020-06-04
<Dialog
open={open}
onClose={onClose}
onDelete={onDelete}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
对话框组件没有 prop
onDelete
https://material-ui.com/api/dialog/
Nikita Madeev
2020-06-04