When I click Link, I want to show a modal to ask user, if they really want to make the change. If they click "YES", I want continue to redirect to other page.
Here is my code:
import { Modal } from 'antd';
import { Link } from 'react-router-dom';
import React from 'react';
const confirm = Modal.confirm;
class Menu extends CLComponent {
constructor (props) {
super(props);
this.handleClick = this.handleClick.bind(this);
this.showModal = this.showModal.bind(this);
this.confirmChange = this.confirmChange.bind(this);
}
linkClick (e) {
let hashValue = e.target.getAttribute("href");
e.preventDefault();
e.stopPropagation();
}
showModal (url) {
const that = this;
confirm({
title: 'Sure leave?',
content: "Are you sure to leave this page?",
onOk() {
that.confirmChange(url);
},
onCancel() {
console.log('Cancel');
},
});
}
confirmChange () {
location.hash = e;
}
render() {
return (
<div>
<Link onClick={ this.linkClick } to={"/test"}>test</Link>
</div>
);
}
}
Altrough I make it work, but I felt it is so bad. Is there any way to change my code?