Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c0ae2041
Unverified
Commit
c0ae2041
authored
Nov 01, 2020
by
Jan Potoms
Committed by
GitHub
Nov 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid calling setState in callback ref (#18589)
parent
a6759c62
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
33 deletions
+100
-33
build_test_deploy.yml
.github/workflows/build_test_deploy.yml
+5
-3
link.tsx
packages/next/client/link.tsx
+32
-30
click-away-race-condition.js
test/integration/link-ref/pages/click-away-race-condition.js
+50
-0
index.test.js
test/integration/link-ref/test/index.test.js
+13
-0
No files found.
.github/workflows/build_test_deploy.yml
View file @
c0ae2041
...
...
@@ -107,10 +107,12 @@ jobs:
steps
:
-
uses
:
actions/checkout@v2
-
run
:
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
-
run
:
cat package
s/next/package.json | jq '.resolutions.webpack = "^5.0.0-beta.30"' > package.json.tmp && mv package.json.tmp packages/next/
package.json
-
run
:
cat package
s/next/package.json | jq '.resolutions.react = "^17.0.0-rc.1"' > package.json.tmp && mv package.json.tmp packages/next/
package.json
-
run
:
cat package
s/next/package.json | jq '.resolutions."react-dom" = "^17.0.0-rc.1"' > package.json.tmp && mv package.json.tmp packages/next/
package.json
-
run
:
cat package
.json | jq '.resolutions.webpack = "^5.0.0-beta.30"' > package.json.tmp && mv package.json.tmp
package.json
-
run
:
cat package
.json | jq '.resolutions.react = "^17.0.1"' > package.json.tmp && mv package.json.tmp
package.json
-
run
:
cat package
.json | jq '.resolutions."react-dom" = "^17.0.1"' > package.json.tmp && mv package.json.tmp
package.json
-
run
:
yarn install --check-files
-
run
:
yarn list webpack react react-dom
-
run
:
node run-tests.js test/integration/link-ref/test/index.test.js
-
run
:
node run-tests.js test/integration/production/test/index.test.js
-
run
:
node run-tests.js test/integration/basic/test/index.test.js
-
run
:
node run-tests.js test/integration/async-modules/test/index.test.js
...
...
packages/next/client/link.tsx
View file @
c0ae2041
...
...
@@ -261,8 +261,6 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
}
const
p
=
props
.
prefetch
!==
false
const
[
childElm
,
setChildElm
]
=
React
.
useState
<
Element
>
()
const
router
=
useRouter
()
const
pathname
=
(
router
&&
router
.
pathname
)
||
'/'
...
...
@@ -276,24 +274,6 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
}
},
[
pathname
,
props
.
href
,
props
.
as
])
React
.
useEffect
(()
=>
{
if
(
p
&&
IntersectionObserver
&&
childElm
&&
childElm
.
tagName
&&
isLocalURL
(
href
)
)
{
// Join on an invalid URI character
const
isPrefetched
=
prefetched
[
href
+
'%'
+
as
]
if
(
!
isPrefetched
)
{
return
listenToIntersections
(
childElm
,
()
=>
{
prefetch
(
router
,
href
,
as
)
})
}
}
},
[
p
,
childElm
,
href
,
as
,
router
])
let
{
children
,
replace
,
shallow
,
scroll
,
locale
}
=
props
// Deprecated. Warning shown by propType check. If the children provided is a string (<Link>example</Link>) we wrap it in an <a> tag
if
(
typeof
children
===
'string'
)
{
...
...
@@ -302,22 +282,44 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
// This will return the first child, if multiple are provided it will throw an error
const
child
:
any
=
Children
.
only
(
children
)
const
childRef
:
any
=
child
&&
typeof
child
===
'object'
&&
child
.
ref
const
cleanup
=
React
.
useRef
<
()
=>
void
>
()
const
setRef
=
React
.
useCallback
(
(
el
:
Element
)
=>
{
// cleanup previous event handlers
if
(
cleanup
.
current
)
{
cleanup
.
current
()
cleanup
.
current
=
undefined
}
if
(
p
&&
IntersectionObserver
&&
el
&&
el
.
tagName
&&
isLocalURL
(
href
))
{
// Join on an invalid URI character
const
isPrefetched
=
prefetched
[
href
+
'%'
+
as
]
if
(
!
isPrefetched
)
{
cleanup
.
current
=
listenToIntersections
(
el
,
()
=>
{
prefetch
(
router
,
href
,
as
)
})
}
}
if
(
childRef
)
{
if
(
typeof
childRef
===
'function'
)
childRef
(
el
)
else
if
(
typeof
childRef
===
'object'
)
{
childRef
.
current
=
el
}
}
},
[
p
,
childRef
,
href
,
as
,
router
]
)
const
childProps
:
{
onMouseEnter
?:
React
.
MouseEventHandler
onClick
:
React
.
MouseEventHandler
href
?:
string
ref
?:
any
}
=
{
ref
:
(
el
:
any
)
=>
{
if
(
el
)
setChildElm
(
el
)
if
(
child
&&
typeof
child
===
'object'
&&
child
.
ref
)
{
if
(
typeof
child
.
ref
===
'function'
)
child
.
ref
(
el
)
else
if
(
typeof
child
.
ref
===
'object'
)
{
child
.
ref
.
current
=
el
}
}
},
ref
:
setRef
,
onClick
:
(
e
:
React
.
MouseEvent
)
=>
{
if
(
child
.
props
&&
typeof
child
.
props
.
onClick
===
'function'
)
{
child
.
props
.
onClick
(
e
)
...
...
test/integration/link-ref/pages/click-away-race-condition.js
0 → 100644
View file @
c0ae2041
import
React
,
{
useCallback
,
useEffect
,
useRef
,
useState
}
from
'react'
import
Link
from
'next/link'
const
useClickAway
=
(
ref
,
onClickAway
)
=>
{
useEffect
(()
=>
{
const
handler
=
(
event
)
=>
{
const
el
=
ref
.
current
// when menu is open and clicked inside menu, A is expected to be false
// when menu is open and clicked outside menu, A is expected to be true
console
.
log
(
'A'
,
el
&&
!
el
.
contains
(
event
.
target
))
el
&&
!
el
.
contains
(
event
.
target
)
&&
onClickAway
(
event
)
}
document
.
addEventListener
(
'click'
,
handler
)
return
()
=>
{
document
.
removeEventListener
(
'click'
,
handler
)
}
},
[
onClickAway
,
ref
])
}
export
default
function
App
()
{
const
[
open
,
setOpen
]
=
useState
(
false
)
const
menuRef
=
useRef
(
null
)
const
onClickAway
=
useCallback
(()
=>
{
console
.
log
(
'click away, open'
,
open
)
if
(
open
)
{
setOpen
(
false
)
}
},
[
open
])
useClickAway
(
menuRef
,
onClickAway
)
return
(
<
div
>
<
div
id
=
"click-me"
onClick
=
{()
=>
setOpen
(
true
)}
>
Open
Menu
<
/div
>
{
open
&&
(
<
div
ref
=
{
menuRef
}
id
=
"the-menu"
>
<
Link
href
=
"/"
>
some
link
<
/Link
>
<
/div
>
)}
<
/div
>
)
}
test/integration/link-ref/test/index.test.js
View file @
c0ae2041
...
...
@@ -49,6 +49,15 @@ const didPrefetch = async (pathname) => {
await
browser
.
close
()
}
function
runCommonTests
()
{
// See https://github.com/vercel/next.js/issues/18437
it
(
'should not have a race condition with a click handler'
,
async
()
=>
{
const
browser
=
await
webdriver
(
appPort
,
'/click-away-race-condition'
)
await
browser
.
elementByCss
(
'#click-me'
).
click
()
await
browser
.
waitForElementByCss
(
'#the-menu'
)
})
}
describe
(
'Invalid hrefs'
,
()
=>
{
describe
(
'dev mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
...
...
@@ -57,6 +66,8 @@ describe('Invalid hrefs', () => {
})
afterAll
(()
=>
killApp
(
app
))
runCommonTests
()
it
(
'should not show error for function component with forwardRef'
,
async
()
=>
{
await
noError
(
'/function'
)
})
...
...
@@ -82,6 +93,8 @@ describe('Invalid hrefs', () => {
})
afterAll
(()
=>
killApp
(
app
))
runCommonTests
()
it
(
'should preload with forwardRef'
,
async
()
=>
{
await
didPrefetch
(
'/function'
)
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment