Unverified Commit 42c01ddc authored by shinkj11's avatar shinkj11 Committed by GitHub

Fix `next/image` usage of `onError()` (#36305)



## History
In PR #35889, `onError` prop was added explicitly on the `Image` component, but it was ommitted from the `ImageElement` component. In result, the callback didn't work at all.

## Now
This PR deletes the `onError` prop from explicitly defined props. Explicitly adding `onError` on the `const imgElementArgs` is considered, but deleting the defined prop from `Image` is more reasonable because the prop isn't used in `Image` component.


## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
Co-authored-by: 's avatarSteven <229881+styfle@users.noreply.github.com>
parent 94faeec1
...@@ -368,7 +368,6 @@ export default function Image({ ...@@ -368,7 +368,6 @@ export default function Image({
objectFit, objectFit,
objectPosition, objectPosition,
onLoadingComplete, onLoadingComplete,
onError,
placeholder = 'empty', placeholder = 'empty',
blurDataURL, blurDataURL,
...all ...all
......
import { useState } from 'react'
import Image from 'next/image'
const Page = () => {
return (
<div>
<h1>Test onError</h1>
<p>
If error occured while loading image, native onError should be called.
</p>
<ImageWithMessage id="1" src="/test.png" layout="fill" />
<ImageWithMessage id="2" src="/nonexistent-img.png" layout="fill" />
<div id="footer" />
</div>
)
}
function ImageWithMessage({ id, ...props }) {
const [msg, setMsg] = useState('no error occured')
const style =
props.layout === 'fill'
? { position: 'relative', width: '64px', height: '64px' }
: {}
return (
<>
<div className="wrap" style={style}>
<Image
id={`img${id}`}
onError={(e) => {
const msg = `error occured while loading ${e.target.id}`
setMsg(msg)
}}
{...props}
/>
</div>
<p id={`msg${id}`}>{msg}</p>
<hr />
</>
)
}
export default Page
...@@ -363,6 +363,28 @@ function runTests(mode) { ...@@ -363,6 +363,28 @@ function runTests(mode) {
) )
}) })
it('should callback native onError when error occured while loading image', async () => {
let browser = await webdriver(appPort, '/on-error')
await check(
() => browser.eval(`document.getElementById("img1").currentSrc`),
/test\.png/
)
await check(
() => browser.eval(`document.getElementById("img2").currentSrc`),
//This is an empty data url
/nonexistent-img\.png/
)
await check(
() => browser.eval(`document.getElementById("msg1").textContent`),
'no error occured'
)
await check(
() => browser.eval(`document.getElementById("msg2").textContent`),
'error occured while loading img2'
)
})
it('should work with image with blob src', async () => { it('should work with image with blob src', async () => {
let browser let browser
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment