Unverified Commit d23b1601 authored by lijianan's avatar lijianan Committed by GitHub

style: remove useless React.Fragment tag on `next/image` (#48997)



### What?

remove useless React.Fragment tag

### Why?

<img /> is a single element, No need `<></>` as an outer wrapper

### How?
parent 25ba8a74
...@@ -418,101 +418,96 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>( ...@@ -418,101 +418,96 @@ const ImageElement = forwardRef<HTMLImageElement | null, ImageElementProps>(
) => { ) => {
loading = isLazy ? 'lazy' : loading loading = isLazy ? 'lazy' : loading
return ( return (
<> <img
<img {...rest}
{...rest} {...getDynamicProps(fetchPriority)}
{...getDynamicProps(fetchPriority)} loading={loading}
loading={loading} width={widthInt}
width={widthInt} height={heightInt}
height={heightInt} decoding="async"
decoding="async" data-nimg={fill ? 'fill' : '1'}
data-nimg={fill ? 'fill' : '1'} className={className}
className={className} style={{ ...imgStyle, ...blurStyle }}
style={{ ...imgStyle, ...blurStyle }} // It's intended to keep `loading` before `src` because React updates
// It's intended to keep `loading` before `src` because React updates // props in order which causes Safari/Firefox to not lazy load properly.
// props in order which causes Safari/Firefox to not lazy load properly. // See https://github.com/facebook/react/issues/25883
// See https://github.com/facebook/react/issues/25883 {...imgAttributes}
{...imgAttributes} ref={useCallback(
ref={useCallback( (img: ImgElementWithDataProp | null) => {
(img: ImgElementWithDataProp | null) => { if (forwardedRef) {
if (forwardedRef) { if (typeof forwardedRef === 'function') forwardedRef(img)
if (typeof forwardedRef === 'function') forwardedRef(img) else if (typeof forwardedRef === 'object') {
else if (typeof forwardedRef === 'object') { // @ts-ignore - .current is read only it's usually assigned by react internally
// @ts-ignore - .current is read only it's usually assigned by react internally forwardedRef.current = img
forwardedRef.current = img
}
}
if (!img) {
return
} }
if (onError) { }
// If the image has an error before react hydrates, then the error is lost. if (!img) {
// The workaround is to wait until the image is mounted which is after hydration, return
// then we set the src again to trigger the error handler (if there was an error). }
// eslint-disable-next-line no-self-assign if (onError) {
img.src = img.src // If the image has an error before react hydrates, then the error is lost.
} // The workaround is to wait until the image is mounted which is after hydration,
if (process.env.NODE_ENV !== 'production') { // then we set the src again to trigger the error handler (if there was an error).
if (!srcString) { // eslint-disable-next-line no-self-assign
console.error( img.src = img.src
`Image is missing required "src" property:`, }
img if (process.env.NODE_ENV !== 'production') {
) if (!srcString) {
} console.error(`Image is missing required "src" property:`, img)
if (img.getAttribute('alt') === null) {
console.error(
`Image is missing required "alt" property. Please add Alternative Text to describe the image for screen readers and search engines.`
)
}
} }
if (img.complete) { if (img.getAttribute('alt') === null) {
handleLoading( console.error(
img, `Image is missing required "alt" property. Please add Alternative Text to describe the image for screen readers and search engines.`
srcString,
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete,
unoptimized
) )
} }
},
[
srcString,
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete,
onError,
unoptimized,
forwardedRef,
]
)}
onLoad={(event) => {
const img = event.currentTarget as ImgElementWithDataProp
handleLoading(
img,
srcString,
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete,
unoptimized
)
}}
onError={(event) => {
// if the real image fails to load, this will ensure "alt" is visible
setShowAltText(true)
if (placeholder === 'blur') {
// If the real image fails to load, this will still remove the placeholder.
setBlurComplete(true)
} }
if (onError) { if (img.complete) {
onError(event) handleLoading(
img,
srcString,
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete,
unoptimized
)
} }
}} },
/> [
</> srcString,
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete,
onError,
unoptimized,
forwardedRef,
]
)}
onLoad={(event) => {
const img = event.currentTarget as ImgElementWithDataProp
handleLoading(
img,
srcString,
placeholder,
onLoadRef,
onLoadingCompleteRef,
setBlurComplete,
unoptimized
)
}}
onError={(event) => {
// if the real image fails to load, this will ensure "alt" is visible
setShowAltText(true)
if (placeholder === 'blur') {
// If the real image fails to load, this will still remove the placeholder.
setBlurComplete(true)
}
if (onError) {
onError(event)
}
}}
/>
) )
} }
) )
...@@ -546,7 +541,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>( ...@@ -546,7 +541,7 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
forwardedRef forwardedRef
) => { ) => {
const configContext = useContext(ImageConfigContext) const configContext = useContext(ImageConfigContext)
const config: ImageConfig = useMemo(() => { const config = useMemo<ImageConfig>(() => {
const c = configEnv || configContext || imageConfigDefault const c = configEnv || configContext || imageConfigDefault
const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b) const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b)
const deviceSizes = c.deviceSizes.sort((a, b) => a - b) const deviceSizes = c.deviceSizes.sort((a, b) => a - b)
......
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