Problem description: In add-raw-dialog component, there is an image upload function, and it references the sub-component item-image to realize the function, but now the image cannot be previewed normally after uploading. In item-form, item-image is also referenced to realize the same function, but item-form images can be displayed normally.
The git repository for the code in question is [https://github.com/liugan2222/demo], and the associated components is
[(https://github.com/liugan2222/demo/blob/master/components/add-dialog/add-raw-dialog.tsx) (https://github.com/liugan2222/demo/blob/master/components/form/item-form.tsx) (https://github.com/liugan2222/demo/blob/master/components/common/item/item-image.tsx)]
Note: There are some differences between the repository code and my local code (the code snippet below is my local code), but the problem in [add-raw-dialog] with the image not loading properly after uploading is the same.
》》》》》》》》》》》》》》》
img is not loading the code properly【for add-item】:
Parent Component References:
{
try {
form.setValue(items.${index}.smallImageUrl, "uploading...")
const pictureObj = await uploadFile(file)
form.setValue(items.${index}.smallImageUrl, pictureObj.data.id, {
shouldDirty: true,
shouldValidate: true
})
} catch (error) {
form.setValue(items.${index}.smallImageUrl, null)
form.setError(items.${index}.picture, {
type: 'manual',
message: error instanceof Error ? error.message : 'File upload failed'
})
}
}}
/>
Subcomponent handling: const smallImageUrl = useWatch({ control: form.control, name: fieldName, exact: true });
// Update image URL when smallImageUrl changesuseEffect(() => { if (smallImageUrl === "uploading...") { setIsUploading(true) return }
setIsUploading(false)setImageLoaded(false)
if (smallImageUrl) { setUniqueKey([Date.now](https://date.now)() + Math.random()) try { const baseUrl = `https://fp.ablueforce.com/api/files/${smallImageUrl}/media` // Add timestamp to avoid caching const cacheBuster = [Date.now](https://date.now)().toString(36) + Math.random().toString(36).substr(2, 5) const newImageUrl = `${baseUrl}?v=${cacheBuster}` setImageUrl(newImageUrl)
// Increment the key to force a complete re-render of the Image component } catch (error) { console.error("Error setting image URL:", error) setImageUrl(DEFAULT_IMAGE) }
} else { setImageUrl(DEFAULT_IMAGE)}}, [smallImageUrl])
{isEditing && (
{/* Always render the AspectRatio container to ensure consistent layout */} {isUploading ? (
Uploading image...
) : ( { console.error('Image load failed:', imageUrl); ([e.target](https://e.target) as HTMLImageElement).src = DEFAULT_IMAGE; }} /> )}
{/* Position buttons vertically on the right side */} {smallImageUrl && !isUploading && (
Replace Remove
)}
)} img normal loading code【for edit-item】: Parent Component References:
{ try { // 先设置loading状态 form.setValue("smallImageUrl", "uploading...") const response = await uploadFile(file) form.setValue("smallImageUrl", [response.data.id](https://response.data.id)) } catch (error) { form.setValue("smallImageUrl", null) console.error("Error uploading file:", error) } }} 》》》》》》》》》》》》》》》》》》》》》》》》》 When add-item is uploaded, the image fails to load and display properly after uploading, but the image url is normal when viewed in developer mode [interface uploadFile and form assignments are fine].
"next": "15.0.3", "react": "^18.2.0", "react-hook-form": "^7.54.2", "react-image-crop": "^11.0.7",
》》》》》》》》》》》》》》》》》》》》》》》 Please help me to realize that when add-raw-dialog is uploaded, the image can be previewed and displayed properly, thank you very much!