diff --git a/packages/next/client/link.tsx b/packages/next/client/link.tsx index 2aba7f79139d8b8..9a7f65b52997b92 100644 --- a/packages/next/client/link.tsx +++ b/packages/next/client/link.tsx @@ -284,6 +284,13 @@ function Link(props: React.PropsWithChildren) { } = { ref: setRef, onClick: (e: React.MouseEvent) => { + if (process.env.NODE_ENV !== 'production') { + if (!e) { + throw new Error( + `Component rendered inside next/link has to pass click event to "onClick" prop.` + ) + } + } if (child.props && typeof child.props.onClick === 'function') { child.props.onClick(e) } diff --git a/test/integration/client-navigation/pages/link-invalid-onclick.js b/test/integration/client-navigation/pages/link-invalid-onclick.js new file mode 100644 index 000000000000000..7cebe86d2895df2 --- /dev/null +++ b/test/integration/client-navigation/pages/link-invalid-onclick.js @@ -0,0 +1,35 @@ +import Link from 'next/link' +import { useState } from 'react' + +export default function Page(props) { + const [errorCount, setErrorCount] = useState(0) + + function Button(props) { + return ( + { + e.preventDefault() + try { + props.onClick() + } catch (err) { + setErrorCount(errorCount + 1) + console.error(err) + } + }} + > + {props.href} + + ) + } + + return ( + <> +

{errorCount}

+ +