Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require component rendered as child of Link to pass event to onClick handler #27723

Merged
merged 3 commits into from Feb 7, 2022

Conversation

pie6k
Copy link
Contributor

@pie6k pie6k commented Aug 3, 2021

Currently, if you render next/link like

<Link>
  <CustomComponent />
</Link>

and have

interface Props {
  onClick?: () => void; // <— note we're not passing event as an argument
}

function CustomComponent({ onClick }: Props) {
  return <div onClick={() => onClick?.()}>Hello</div>
}

It'll result in error

link.js?f421:21 Uncaught TypeError: Cannot read property 'defaultPrevented' of undefined

Bug

  • Related issues linked using fixes #number
  • Integration tests added
  • Errors have helpful link attached, see contributing.md

Feature

  • Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
  • Related issues linked using fixes #number
  • Integration tests added
  • Documentation added
  • Telemetry added. In case of a feature if it's used or not.
  • Errors have helpful link attached, see contributing.md

Documentation / Examples

  • Make sure the linting passes

…ck` handler

Currently, if you render `next/link` like

```ts
<Link>
  <CustomComponent />
</Link>
```

and have 

```tsx
interface Props {
  onClick?: () => void; // <— note we're not passing event as an argument
}

function CustomComponent({ onClick }: Props) {
  return <div onClick={() => onClick?.()}>Hello</div>
}
```

It'll result in error

```
link.js?f421:21 Uncaught TypeError: Cannot read property 'defaultPrevented' of undefined
```
packages/next/client/link.tsx Outdated Show resolved Hide resolved
Copy link
Member

@styfle styfle left a comment

We need an integration test to confirm this is working as expected

@ijjk

This comment has been minimized.

@ijjk
Copy link
Member

@ijjk ijjk commented Feb 6, 2022

Stats from current PR

Default Build (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary pie6k/next.js patch-1 Change
buildDuration 19.8s 19.7s -94ms
buildDurationCached 7.5s 7.4s -122ms
nodeModulesSize 359 MB 359 MB ⚠️ +385 B
Page Load Tests Overall increase ✓
vercel/next.js canary pie6k/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 4.403 4.344 -0.06
/ avg req/sec 567.75 575.49 +7.74
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2.065 1.997 -0.07
/error-in-render avg req/sec 1210.82 1252.01 +41.19
Client Bundles (main, webpack, commons)
vercel/next.js canary pie6k/next.js patch-1 Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42 kB 42 kB
main-HASH.js gzip 27.4 kB 27.4 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71 kB 71 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary pie6k/next.js patch-1 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary pie6k/next.js patch-1 Change
_app-HASH.js gzip 1.36 kB 1.36 kB
_error-HASH.js gzip 194 B 194 B
amp-HASH.js gzip 312 B 312 B
css-HASH.js gzip 326 B 326 B
dynamic-HASH.js gzip 2.57 kB 2.57 kB
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 919 B 919 B
image-HASH.js gzip 4.94 kB 4.94 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.2 kB 2.2 kB
routerDirect..HASH.js gzip 321 B 321 B
script-HASH.js gzip 383 B 383 B
withRouter-HASH.js gzip 318 B 318 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.6 kB 14.6 kB
Client Build Manifests
vercel/next.js canary pie6k/next.js patch-1 Change
_buildManifest.js gzip 460 B 460 B
Overall change 460 B 460 B
Rendered Page Sizes
vercel/next.js canary pie6k/next.js patch-1 Change
index.html gzip 531 B 531 B
link.html gzip 545 B 545 B
withRouter.html gzip 526 B 526 B
Overall change 1.6 kB 1.6 kB

Diffs

Diff for _buildManifest.js
@@ -13,7 +13,7 @@ self.__BUILD_MANIFEST = {
   "/head": ["static\u002Fchunks\u002Fpages\u002Fhead-7100d3b2a548f0e4.js"],
   "/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-538d621a0e670391.js"],
   "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-f97943edf7ae3dd3.js"],
-  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-27386a147a7100fa.js"],
+  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-34eb165692af8fd9.js"],
   "/routerDirect": [
     "static\u002Fchunks\u002Fpages\u002FrouterDirect-76232dd6bc335a24.js"
   ],
Diff for link-HASH.js
@@ -260,6 +260,8 @@
         var childProps = {
           ref: setRef,
           onClick: function(e) {
+            if (false) {
+            }
             if (child.props && typeof child.props.onClick === "function") {
               child.props.onClick(e);
             }
Diff for link.html
@@ -27,7 +27,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/pages/link-27386a147a7100fa.js"
+      src="/_next/static/chunks/pages/link-34eb165692af8fd9.js"
       defer=""
     ></script>
     <script src="/_next/static/BUILD_ID/_buildManifest.js" defer=""></script>

Default Build with SWC (Increase detected ⚠️)
General Overall increase ⚠️
vercel/next.js canary pie6k/next.js patch-1 Change
buildDuration 23.9s 24.2s ⚠️ +299ms
buildDurationCached 7.4s 7.4s ⚠️ +5ms
nodeModulesSize 359 MB 359 MB ⚠️ +385 B
Page Load Tests Overall increase ✓
vercel/next.js canary pie6k/next.js patch-1 Change
/ failed reqs 0 0
/ total time (seconds) 4.414 4.344 -0.07
/ avg req/sec 566.33 575.5 +9.17
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 2.024 2.03 ⚠️ +0.01
/error-in-render avg req/sec 1235.29 1231.44 ⚠️ -3.85
Client Bundles (main, webpack, commons)
vercel/next.js canary pie6k/next.js patch-1 Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.1 kB 42.1 kB
main-HASH.js gzip 27.4 kB 27.4 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.2 kB 71.2 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary pie6k/next.js patch-1 Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary pie6k/next.js patch-1 Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 180 B 180 B
amp-HASH.js gzip 305 B 305 B
css-HASH.js gzip 321 B 321 B
dynamic-HASH.js gzip 2.56 kB 2.56 kB
head-HASH.js gzip 342 B 342 B
hooks-HASH.js gzip 911 B 911 B
image-HASH.js gzip 4.97 kB 4.97 kB
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.22 kB 2.22 kB
routerDirect..HASH.js gzip 314 B 314 B
script-HASH.js gzip 375 B 375 B
withRouter-HASH.js gzip 309 B 309 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.5 kB 14.5 kB
Client Build Manifests
vercel/next.js canary pie6k/next.js patch-1 Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes
vercel/next.js canary pie6k/next.js patch-1 Change
index.html gzip 534 B 534 B
link.html gzip 547 B 547 B
withRouter.html gzip 528 B 528 B
Overall change 1.61 kB 1.61 kB

Diffs

Diff for _buildManifest.js
@@ -13,7 +13,7 @@ self.__BUILD_MANIFEST = {
   "/head": ["static\u002Fchunks\u002Fpages\u002Fhead-7100d3b2a548f0e4.js"],
   "/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-538d621a0e670391.js"],
   "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-f97943edf7ae3dd3.js"],
-  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-27386a147a7100fa.js"],
+  "/link": ["static\u002Fchunks\u002Fpages\u002Flink-34eb165692af8fd9.js"],
   "/routerDirect": [
     "static\u002Fchunks\u002Fpages\u002FrouterDirect-76232dd6bc335a24.js"
   ],
Diff for link-HASH.js
@@ -260,6 +260,8 @@
         var childProps = {
           ref: setRef,
           onClick: function(e) {
+            if (false) {
+            }
             if (child.props && typeof child.props.onClick === "function") {
               child.props.onClick(e);
             }
Diff for link.html
@@ -27,7 +27,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/pages/link-27386a147a7100fa.js"
+      src="/_next/static/chunks/pages/link-34eb165692af8fd9.js"
       defer=""
     ></script>
     <script src="/_next/static/BUILD_ID/_buildManifest.js" defer=""></script>
Commit: cf44f9b

ijjk
ijjk approved these changes Feb 7, 2022
Copy link
Member

@ijjk ijjk left a comment

Thanks for the PR!

@kodiakhq kodiakhq bot merged commit 41706d6 into vercel:canary Feb 7, 2022
42 checks passed
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
…ck` handler (vercel#27723)

Currently, if you render `next/link` like

```ts
<Link>
  <CustomComponent />
</Link>
```

and have 

```tsx
interface Props {
  onClick?: () => void; // <— note we're not passing event as an argument
}

function CustomComponent({ onClick }: Props) {
  return <div onClick={() => onClick?.()}>Hello</div>
}
```

It'll result in error

```
link.js?f421:21 Uncaught TypeError: Cannot read property 'defaultPrevented' of undefined
```

<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants