Skip to content

Commit

Permalink
DOP-3106: Prevent Images from repeatedly re-rendering (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayangler authored Jul 5, 2022
1 parent dfbbaf1 commit 3f56fd6
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/components/Image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect } from 'react';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import PropTypes from 'prop-types';
import { withPrefix } from 'gatsby';
import { css } from '@emotion/react';
Expand All @@ -10,13 +10,7 @@ const Image = ({ nodeData, handleImageLoaded, className }) => {
const [width, setWidth] = useState(null);
const imgRef = useRef();

useEffect(() => {
if (imgRef.current && imgRef.current.complete) {
handleLoad();
}
});

const handleLoad = () => {
const handleLoad = useCallback(() => {
const img = imgRef.current;
handleImageLoaded(img);

Expand All @@ -29,7 +23,13 @@ const Image = ({ nodeData, handleImageLoaded, className }) => {
if (height) setHeight(height);
if (width) setWidth(height);
}
};
}, [handleImageLoaded, nodeData]);

useEffect(() => {
if (imgRef.current && imgRef.current.complete) {
handleLoad();
}
}, [handleLoad]);

const scaleSize = (width, height, scale) => {
const scaleValue = parseInt(scale, 10) / 100.0;
Expand Down

0 comments on commit 3f56fd6

Please sign in to comment.