切换胶片后,仍然会有图像残留在新胶片的viewport中。
// Film.tsx 中的问题代码
const generateCell = (imageId, indexOfCell) => {
return (<ViewportContainer imageId={imageId} currentFilm={currentFilm} indexOfCell={indexOfCell} />);
};
问题:没有为ViewportContainer提供key属性,导致React在切换胶片时复用组件实例,只更新props。
虽然按胶片ID创建独立的渲染引擎,但切换回之前访问过的胶片时,该渲染引擎可能仍保留之前的viewport状态。
当切换胶片时,旧的DcmCell组件可能没有在正确的时机卸载和清理viewport。
// Film.tsx 修复
const generateCell = (imageId, indexOfCell) => {
const uniqueKey = `${currentFilm.id}-${indexOfCell}`;
return (
<ViewportContainer
key={uniqueKey}
imageId={imageId}
currentFilm={currentFilm}
indexOfCell={indexOfCell}
/>
);
};
// 选择一:胶片切换时强制清理所有viewport
// 选择二:改进viewport的清理逻辑,确保完全清理
在切换胶片时,主动清理当前显示的所有viewport,确保状态重置。