Skip to content

Commit 82ce94c

Browse files
MeiKatztimdorr
authored andcommittedApr 20, 2019
prevent reload of page if an error occurs in onClick event handler (#6711)
* prevent reload of page if an error occurs in onClick event handler in <Link /> 1. catch exception of onClick 2. prevent default event handler 3. re-throw exception * add tests * remove unused Component import in <Link /> test
1 parent 7bd1407 commit 82ce94c

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"esm/react-router-dom.js": {
3-
"bundled": 8076,
4-
"minified": 4865,
5-
"gzipped": 1626,
3+
"bundled": 8159,
4+
"minified": 4903,
5+
"gzipped": 1641,
66
"treeshaked": {
77
"rollup": {
88
"code": 453,
@@ -14,13 +14,13 @@
1414
}
1515
},
1616
"umd/react-router-dom.js": {
17-
"bundled": 158978,
18-
"minified": 56593,
19-
"gzipped": 16361
17+
"bundled": 159106,
18+
"minified": 56622,
19+
"gzipped": 16367
2020
},
2121
"umd/react-router-dom.min.js": {
22-
"bundled": 95833,
23-
"minified": 33613,
24-
"gzipped": 9925
22+
"bundled": 95961,
23+
"minified": 33642,
24+
"gzipped": 9929
2525
}
2626
}

‎packages/react-router-dom/modules/Link.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ function isModifiedEvent(event) {
1313
*/
1414
class Link extends React.Component {
1515
handleClick(event, history) {
16-
if (this.props.onClick) this.props.onClick(event);
16+
try {
17+
if (this.props.onClick) this.props.onClick(event);
18+
} catch (ex) {
19+
event.preventDefault();
20+
throw ex;
21+
}
1722

1823
if (
1924
!event.defaultPrevented && // onClick prevented default

‎packages/react-router-dom/modules/__tests__/Link-test.js

+37
Original file line numberDiff line numberDiff line change
@@ -276,5 +276,42 @@ describe("A <Link>", () => {
276276

277277
expect(memoryHistory.push).toBeCalledTimes(0);
278278
});
279+
280+
it("prevents the default event handler if an error occurs", () => {
281+
const memoryHistory = createMemoryHistory();
282+
memoryHistory.push = jest.fn();
283+
const error = new Error();
284+
const clickHandler = () => {
285+
throw error;
286+
};
287+
const mockPreventDefault = jest.fn();
288+
const to = "/the/path?the=query#the-hash";
289+
290+
renderStrict(
291+
<Router history={memoryHistory}>
292+
<Link to={to} onClick={clickHandler}>
293+
link
294+
</Link>
295+
</Router>,
296+
node
297+
);
298+
299+
console.error = jest.fn(); // keep console clean. Dunno why the catch doesn't do the job correctly.
300+
try {
301+
const a = node.querySelector("a");
302+
ReactTestUtils.Simulate.click(a, {
303+
defaultPrevented: false,
304+
preventDefault: mockPreventDefault,
305+
button: 1
306+
});
307+
} catch (e) {
308+
expect(e).toBe(error);
309+
}
310+
311+
console.error.mockRestore();
312+
expect(clickHandler).toThrow(error);
313+
expect(mockPreventDefault).toHaveBeenCalled();
314+
expect(memoryHistory.push).toBeCalledTimes(0);
315+
});
279316
});
280317
});
+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"esm/react-router.js": {
3-
"bundled": 23396,
4-
"minified": 13250,
5-
"gzipped": 3680,
3+
"bundled": 23435,
4+
"minified": 13241,
5+
"gzipped": 3679,
66
"treeshaked": {
77
"rollup": {
88
"code": 2214,
@@ -14,13 +14,13 @@
1414
}
1515
},
1616
"umd/react-router.js": {
17-
"bundled": 98991,
18-
"minified": 35022,
19-
"gzipped": 11227
17+
"bundled": 99032,
18+
"minified": 35013,
19+
"gzipped": 11222
2020
},
2121
"umd/react-router.min.js": {
22-
"bundled": 61594,
23-
"minified": 21423,
24-
"gzipped": 7606
22+
"bundled": 61635,
23+
"minified": 21414,
24+
"gzipped": 7600
2525
}
2626
}

0 commit comments

Comments
 (0)
Please sign in to comment.