-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
326b1f3
commit ca53fff
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
import { useCallback, useRef, useEffect, useState } from 'react' | ||
import { flushSync } from 'react-dom' | ||
|
||
export default function Page() { | ||
const [isVisible, setIsVisible] = useState(true) | ||
|
||
const statusRef = useRef({ wasInitialized: false, wasCleanedUp: false }) | ||
|
||
const refWithCleanup = useCallback((el) => { | ||
if (!el) { | ||
console.error( | ||
'callback refs that returned a cleanup should never be called with null' | ||
) | ||
return | ||
} | ||
|
||
statusRef.current.wasInitialized = true | ||
return () => { | ||
statusRef.current.wasCleanedUp = true | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
const timeout = setTimeout( | ||
() => { | ||
flushSync(() => { | ||
setIsVisible(false) | ||
}) | ||
if (!statusRef.current.wasInitialized) { | ||
console.error('callback ref was not initialized') | ||
} | ||
if (!statusRef.current.wasCleanedUp) { | ||
console.error('callback ref was not cleaned up') | ||
} | ||
}, | ||
100 // if we hide the Link too quickly, the prefetch won't fire, failing a test | ||
) | ||
return () => clearTimeout(timeout) | ||
}, []) | ||
|
||
if (!isVisible) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Link href="/" ref={refWithCleanup}> | ||
Click me | ||
</Link> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/integration/next-image-new/app-dir/app/ref-cleanup/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use client' | ||
import Image from 'next/image' | ||
import { useCallback, useEffect, useState } from 'react' | ||
|
||
export default function Home() { | ||
const [displayImage, setDisplayImage] = useState(true) | ||
|
||
const refWithCleanup = useCallback((el) => { | ||
if (!el) { | ||
throw new Error( | ||
'callback refs that returned a cleanup should never be called with null' | ||
) | ||
} | ||
|
||
return () => { | ||
console.log('callback ref was cleaned up') | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
setDisplayImage(false) | ||
}, []) | ||
|
||
return ( | ||
<main> | ||
<h1>Should call ref cleanup on unmount</h1> | ||
<section> | ||
{displayImage ? ( | ||
<div style={{ position: 'relative', width: 10, height: 10 }}> | ||
<Image | ||
ref={refWithCleanup} | ||
priority | ||
fill | ||
src="/test.jpg" | ||
alt="alt" | ||
sizes="10px" | ||
/> | ||
</div> | ||
) : null} | ||
</section> | ||
</main> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters