5

I have a small project using justified-layout and react-virtualized,

I set the width and height of the preview images with the useCallback of previewImageContainerRef Ref. ( In the <PreviewImage> component [(<Results> / <PreviewImgRow> / <PreviewImage>) components > albums > results > preview_image.js], ref={previewImageContainerRef} is in preview_image.js as well, located on <div> with className="c-flex-center picture-frame-wrapper")

    // useCallback > Ref : previewImageContainerRef
    const previewImageContainerRef = useCallback(node => {

        if (node !== null) {

            // Set > Preview Image Width
            props.setPreviewImageWidth(node.getBoundingClientRect().width);

            // Calculate > Preview Image Height
            let new_height = (node.getBoundingClientRect().width / defaultImageWidthRef.current ) * defaultImageHeightRef.current;

            // Set > Preview Image Height
            props.setPreviewImageHeight(new_height);

        }

    }, []);

The useState for setPreviewImageWidth and setPreviewImageHeight, are defined in the parent component (<Results> [components > albums > results > Results.js]):

  // Preview Image > Width & Height
  const [previewImageWidth, setPreviewImageWidth] = useState(0);
  const [previewImageHeight, setPreviewImageHeight] = useState(0);

justifiedRowDetailsRef is defined in <Results> component, it uses useCallback as well and it is one of the parent <div> wrapping around the preview image (ref={justifiedRowDetailsRef} is in component [(<Results> / <PreviewImgRow>) components > albums > results > PreviewImgRow.js]):

  // useCallback > Ref : justifiedRowDetailsRef
  const justifiedRowDetailsRef = useCallback((node) => {
    if (node !== null) {

      setJustifiedRowDetailsWidth(node.getBoundingClientRect().width);
      setJustifiedRowDetailsHeight(node.getBoundingClientRect().height);

    }
  }, []);

The useState for justifiedRowDetailsWidth and justifiedRowDetailsHeight are defined in <Result> component:

  const [justifiedRowDetailsWidth, setJustifiedRowDetailsWidth] = useState(0);
  const [justifiedRowDetailsHeight, setJustifiedRowDetailsHeight] = useState(0);

justifiedRowDetailsHeight is used in the height calculations for multiple parent <div> wrapping the preview image

The problem I am facing is that the justifiedRowDetailsHeight value is only updated correctly when the image in the <JustifiedBox> component is clicked to expand the second time and after

Preview of the problem:

enter image description here

and here is my codesandbox link: https://codesandbox.io/s/gifted-ride-qvquwv?file=/components/albums/results/Results.js

any asistance on this is greatly appreciated

Edit: I have commented out:

  // useCallback > Ref : justifiedRowDetailsRef
  const justifiedRowDetailsRef = useCallback((node) => {
    if (node !== null) {

      setJustifiedRowDetailsWidth(node.getBoundingClientRect().width);
      setJustifiedRowDetailsHeight(node.getBoundingClientRect().height);

    }
  }, []);

and replaced it with:

const justifiedRowDetailsRef = useRef(null);

and passed setJustifiedRowDetailsWidth and setJustifiedRowDetailsHeight to <PreviewImgRow> component, and added this useEffect() into it:

  useEffect(() => {
    if (justifiedRowDetailsRef.current) {
      console.log("justifiedRowDetailsRef.current > NOT EMPTY");

      setJustifiedRowDetailsWidth(justifiedRowDetailsRef.current.offsetWidth);
      setJustifiedRowDetailsHeight(justifiedRowDetailsRef.current.offsetHeight);
    }
  }, [previewImageHeight, isPreviewShown]);

Now on first click of the image, the height is correct, but if I clicked on images with different dimension ( for example, the square image keep calm and eat food), the height of the preview image is still using the previous preview image height ( the rectangle height ) and one step behind

Steps to reproduce the bug:

Step1:

enter image description here

Step2:

enter image description here

Step3:

enter image description here

Step4:

enter image description here

yeln
  • 462
  • 2
  • 10
  • 23
  • I was unable to reproduce the problem. For me, the preview is shown with the reasonable height both on first and on further openings. Is the question still relevant? – Vasiliy Artamonov Jan 02 '23 at 15:25
  • Yeah the problem isn't being replicated for me too. – Helper Jan 03 '23 at 02:20
  • hi, thanks for the replies, I have updated the question with steps to reproduce the bug – yeln Jan 03 '23 at 02:23
  • any help/hint on this will be very much appreciated – yeln Jan 04 '23 at 09:34
  • You need to reduce the explanation. It's too long and many won't be interested to read. Add a TLDR to summarize in a few words, and only explaining what's the core issue is. – Badal Saibo Jan 05 '23 at 06:11
  • **Cannot reproduce the problem**, also the codesandbox takes too long to load and the code is polluted with unused imports and commented-out blocks. I would suggest you clean that up a bit for anyone who tries to help. I used Chrome, **could you check if you can reproduce the problem on the codesandbox provided** on your browser and on different browsers? – Arturo Mendes Jan 05 '23 at 07:47

0 Answers0