リスクが高いかもしれませんが、少なくとも執筆時点では機能しているようです。本番環境で使用しています。
これは ES6 と React です。テストしたところ、以下のブラウザで動作することがわかりました。ボーナスの 1 つは、例外がある場合 (これを作成する途中でカップルがいた場合)、通常のリンクのようにリンクに移動し<a>
ますが、SPA ではなく、ofc になります。
デスクトップ:
- クローム v.76.0.3809.132
- サファリ v.12.1.2
- Firefox Quantum v.69.0.1
- エッジ 18
- エッジ 17
- IE11
モバイル/タブレット:
- アンドロイド v.8 サムスン インターネット
- アンドロイド v.8 クローム
- アンドロイド v.9 クローム
- iOS11.4 サファリ
- iOS12.1 サファリ
.
import 'mdn-polyfills/MouseEvent'; // for IE11
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
class ProductListLink extends Component {
constructor(props) {
super(props);
this.realClick = true;
this.onProductClick = this.onProductClick.bind(this);
}
onProductClick = (e) => {
const { target, nativeEvent } = e;
const clonedNativeEvent = new MouseEvent('click', nativeEvent);
if (!this.realClick) {
this.realClick = true;
return;
}
e.preventDefault();
e.stopPropagation();
// @todo what you want before the link is acted on here
this.realClick = false;
target.dispatchEvent(clonedNativeEvent);
};
render() {
<Link
onClick={(e => this.onProductClick(e))}
>
Lorem
</Link>
}
}