采用技术react.js函数组件,ant design +antprotable +umi框架实现后台管理系统
使用组件ant design 的气泡弹窗,回调函数传值时,发现出现自调用,并传递的参数不是自己想要的数值,而是自带的e(函数小助手),那么怎样解决自调用问题,和给函数传递自己想要的数值那
<Popconfirm
key="delete"
title="确定删除?"
onConfirm={confirm(record)}
okText="是"
cancelText="否"
>
<a
style={
record.status ? { color: 'rgba(84,164,252)' } : { color: '#ccc' }
}
>
删除
</a>
</Popconfirm>,
}
//这里出现接收到的record不是想要的值并且confirm还会在刷新时自调用
function confirm(record) {
console.log(record, '点击删除的回调函数');
if (record.status) {
delateList({ taskId: record.taskId }).then((res) => {
if (res && res.code == 1) {
actionRef.current && actionRef.current.reload();
} else {
notification.error({
description: '接口请求失败',
message: '接口请求失败',
});
}
});
}
}
原因分析:
因为直接传参数就会引发自调用,在没有声明函数助理e时,传的值会被默认成e值,
解决方案:
在传值时应该在组件回调函数内使用箭头函数将e传给confirm函数像这样
传值的时候也要注意接收e值就可以解决啦