Skip to content

Commit 9d670db

Browse files
Antoine LlorcaAntoine Llorca
authored andcommitted
fix(render): use ref instead of state
1 parent f9e59fe commit 9d670db

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

lib/LayeredImage.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const LayeredImage: React.FC<ILayeredImageProps> = ({
5050
style,
5151
}) => {
5252
const [size, setSize] = useState<Size>({ width: 0, height: 0 })
53-
const [interaction, setInteraction] = useState<Interaction>(Interaction.None)
53+
const interactionRef = useRef<Interaction>(Interaction.None)
5454
const [loaded, setLoaded] = useState<number>(0)
5555
const [, setError] = useState<number>(0)
5656

@@ -119,13 +119,13 @@ export const LayeredImage: React.FC<ILayeredImageProps> = ({
119119
}
120120

121121
const computeStyles = (
122-
_interaction: Interaction = interaction,
122+
interaction: Interaction = interactionRef.current,
123123
event?: React.SyntheticEvent<HTMLDivElement>,
124124
pageX?: number,
125125
pageY?: number,
126126
preventDefault = false,
127127
) => {
128-
const { width, height } = _interaction === Interaction.Resize ? getDimensions() : size
128+
const { width, height } = interaction === Interaction.Resize ? getDimensions() : size
129129

130130
const scrollLeft =
131131
document.documentElement.scrollLeft ||
@@ -200,7 +200,7 @@ export const LayeredImage: React.FC<ILayeredImageProps> = ({
200200
transitionDuration: "0.075s",
201201
},
202202
},
203-
}[_interaction]
203+
}[interaction]
204204

205205
if (preventDefault) {
206206
event.preventDefault()
@@ -216,21 +216,24 @@ export const LayeredImage: React.FC<ILayeredImageProps> = ({
216216
}
217217
}
218218

219-
setSize({ width, height })
220-
setInteraction(_interaction)
219+
if (interaction === Interaction.Resize) {
220+
setSize({ width, height })
221+
}
222+
223+
interactionRef.current = interaction
221224
}
222225

223226
// prettier-ignore
224227
const handleMouseInteraction =
225-
(_interaction?: Interaction) =>
228+
(interaction?: Interaction) =>
226229
(event: React.MouseEvent<HTMLDivElement>) =>
227-
computeStyles(_interaction, event, event.pageX, event.pageY, true);
230+
computeStyles(interaction, event, event.pageX, event.pageY, true);
228231

229232
// prettier-ignore
230233
const handleTouchInteraction =
231-
(_interaction?: Interaction) =>
234+
(interaction?: Interaction) =>
232235
(event: React.TouchEvent<HTMLDivElement>) =>
233-
computeStyles(_interaction, event, event.touches[0].pageX, event.touches[0].pageY);
236+
computeStyles(interaction, event, event.touches[0].pageX, event.touches[0].pageY);
234237

235238
const handleInteractionEnd = () => computeStyles(Interaction.None)
236239

lib/utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
*/
44
export const applyStyles = (element: HTMLDivElement, styles: React.CSSProperties) => {
55
for (const [style, value] of Object.entries(styles)) {
6-
if (isSafariDesktop()) {
7-
element.style[style] = value
8-
} else {
9-
requestAnimationFrame(() => {
10-
element.style[style] = value
11-
})
12-
}
6+
element.style[style] = value
137
}
148
}
159

0 commit comments

Comments
 (0)