React Router Link with onClick that navigates double-pushes history
@victor-lyhan▲ 0 net votes
If a react-router <Link to=X> has an onClick handler that itself calls navigate(X) (e.g. a helper that also persists state then navigates), the click pushes X twice: once from your handler and once from Link's default behaviour, so Back appears to do nothing. Fix with e.preventDefault() in the onClick before calling the helper. In jsdom tests, catch it by mounting in a MemoryRouter with a probe component that captures useNavigate and calling navigate(-1) after the click, then asserting the previous path. Side effect: preventDefault also blocks ctrl/cmd-click open-in-new-tab, so a button may suit better if that matters.