【发布时间】:2021-05-26 08:47:51
【问题描述】:
我正在制作 react, next.js 应用程序,我在该应用程序上显示各种图像:
用户单击左侧列表中的项目,并在右侧获得三张图像。图片基于 next.js Image 组件显示:
<Image
src={visualizationData[0]}
quality={50}
alt="Zdjęcie oryginalne"
width={600}
height={600}
/>
visualizationData 是一个变量,它使用 State Hook 并将 3 个网址返回到用户想要显示的照片:
const [visualizationData, setVisualizationData] = useState(getVisualizationData(props.history.model.name, props.history.dataLists[0].replace('img/', '')));
一切正常,但速度很慢,因为后端需要将 .tif 照片转换为 .jpg 并且大约需要 3 秒。我想在这 3 秒内显示 spinner 或类似于 spinner 的东西,而不是让用户看到旧的可视化。
如何制作?我不知道如何检查照片是否已加载,因为我只是将 url 传递给 Image 组件的 src 属性。
这里是getVisualizationData(modelName, pathAndPhotoName)的代码:
export function getVisualizationData(modelName, pathAndPhotoName) {
const photoName = pathAndPhotoName.split('/').slice(-1)[0];
const datasetPath = pathAndPhotoName.replace(`/${photoName}`, '');
const visualizationData = [
`${backendUrl}download/datasets/${datasetPath}/image/${photoName}`,
`${backendUrl}download/datasets/${datasetPath}/mask/${photoName}`,
`${backendUrl}download/models/${modelName}/Predictions/1/${photoName}`];
return visualizationData;
}
它正在将路径转换为照片并以数组的形式返回。
【问题讨论】:
标签: javascript reactjs next.js react-image