Skip to content

Commit 4810bd2

Browse files
committed
fix(BackgroundImage): Image race condition
1 parent e942e17 commit 4810bd2

File tree

1 file changed

+4
-2
lines changed
  • packages/react/src/components/BackgroundImage

1 file changed

+4
-2
lines changed

packages/react/src/components/BackgroundImage/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const BackgroundImage = forwardRef<Handle, BackgroundImageProps>(
3939
const backgroundImageRef = useRef<FabricImage | null>(null)
4040

4141
const store = useStoreApi()
42+
const newestSrcRef = useRef(src)
4243

4344
const { width, height } = useStore(selector)
4445

@@ -132,13 +133,14 @@ const BackgroundImage = forwardRef<Handle, BackgroundImageProps>(
132133
}
133134
const { domNode, setLoading } = store.getState()
134135
setLoading(true)
136+
newestSrcRef.current = src
137+
135138
FabricImage.fromURL(src, { crossOrigin: 'anonymous' })
136139
.then(imageSource => {
137140
onLoad?.(imageSource)
138141
const currentSrc = imageSource.getSrc()
139-
const latestSrc = backgroundImageRef.current?.getSrc()
140142

141-
if (latestSrc && currentSrc !== latestSrc) {
143+
if (currentSrc !== newestSrcRef.current) {
142144
return
143145
}
144146

0 commit comments

Comments
 (0)