diff --git a/packages/react-router-dom/.size-snapshot.json b/packages/react-router-dom/.size-snapshot.json
index 117255546f..28d85aeac8 100644
--- a/packages/react-router-dom/.size-snapshot.json
+++ b/packages/react-router-dom/.size-snapshot.json
@@ -1,8 +1,8 @@
{
"esm/react-router-dom.js": {
- "bundled": 8076,
- "minified": 4865,
- "gzipped": 1626,
+ "bundled": 8159,
+ "minified": 4903,
+ "gzipped": 1641,
"treeshaked": {
"rollup": {
"code": 453,
@@ -14,13 +14,13 @@
}
},
"umd/react-router-dom.js": {
- "bundled": 158978,
- "minified": 56593,
- "gzipped": 16361
+ "bundled": 159106,
+ "minified": 56622,
+ "gzipped": 16367
},
"umd/react-router-dom.min.js": {
- "bundled": 95833,
- "minified": 33613,
- "gzipped": 9925
+ "bundled": 95961,
+ "minified": 33642,
+ "gzipped": 9929
}
}
diff --git a/packages/react-router-dom/modules/Link.js b/packages/react-router-dom/modules/Link.js
index 47b6802fc5..9779b6aea0 100644
--- a/packages/react-router-dom/modules/Link.js
+++ b/packages/react-router-dom/modules/Link.js
@@ -13,7 +13,12 @@ function isModifiedEvent(event) {
*/
class Link extends React.Component {
handleClick(event, history) {
- if (this.props.onClick) this.props.onClick(event);
+ try {
+ if (this.props.onClick) this.props.onClick(event);
+ } catch (ex) {
+ event.preventDefault();
+ throw ex;
+ }
if (
!event.defaultPrevented && // onClick prevented default
diff --git a/packages/react-router-dom/modules/__tests__/Link-test.js b/packages/react-router-dom/modules/__tests__/Link-test.js
index 7614d1a513..f5831dbafc 100644
--- a/packages/react-router-dom/modules/__tests__/Link-test.js
+++ b/packages/react-router-dom/modules/__tests__/Link-test.js
@@ -276,5 +276,42 @@ describe("A ", () => {
expect(memoryHistory.push).toBeCalledTimes(0);
});
+
+ it("prevents the default event handler if an error occurs", () => {
+ const memoryHistory = createMemoryHistory();
+ memoryHistory.push = jest.fn();
+ const error = new Error();
+ const clickHandler = () => {
+ throw error;
+ };
+ const mockPreventDefault = jest.fn();
+ const to = "/the/path?the=query#the-hash";
+
+ renderStrict(
+
+
+ link
+
+ ,
+ node
+ );
+
+ console.error = jest.fn(); // keep console clean. Dunno why the catch doesn't do the job correctly.
+ try {
+ const a = node.querySelector("a");
+ ReactTestUtils.Simulate.click(a, {
+ defaultPrevented: false,
+ preventDefault: mockPreventDefault,
+ button: 1
+ });
+ } catch (e) {
+ expect(e).toBe(error);
+ }
+
+ console.error.mockRestore();
+ expect(clickHandler).toThrow(error);
+ expect(mockPreventDefault).toHaveBeenCalled();
+ expect(memoryHistory.push).toBeCalledTimes(0);
+ });
});
});
diff --git a/packages/react-router/.size-snapshot.json b/packages/react-router/.size-snapshot.json
index b6f9d9a55f..5870d6a4e1 100644
--- a/packages/react-router/.size-snapshot.json
+++ b/packages/react-router/.size-snapshot.json
@@ -1,8 +1,8 @@
{
"esm/react-router.js": {
- "bundled": 23396,
- "minified": 13250,
- "gzipped": 3680,
+ "bundled": 23435,
+ "minified": 13241,
+ "gzipped": 3679,
"treeshaked": {
"rollup": {
"code": 2214,
@@ -14,13 +14,13 @@
}
},
"umd/react-router.js": {
- "bundled": 98991,
- "minified": 35022,
- "gzipped": 11227
+ "bundled": 99032,
+ "minified": 35013,
+ "gzipped": 11222
},
"umd/react-router.min.js": {
- "bundled": 61594,
- "minified": 21423,
- "gzipped": 7606
+ "bundled": 61635,
+ "minified": 21414,
+ "gzipped": 7600
}
}