react-image icon indicating copy to clipboard operation
react-image copied to clipboard

App freeze when using React 18

Open Myrfion opened this issue 3 years ago • 13 comments

Hello, I upgraded the app to react 18 from reacting 17 and in the places of the app where I use react-image, it gets freezer and stops responding (feels like it gets in some infinite loop). Has anybody else faced something like that?

Myrfion avatar Jun 29 '22 20:06 Myrfion

@Myrfion Was losing my mind the past hour trying to figure out what was happening, looks like I'm not alone 🥵

anthonykrivonos avatar Jun 30 '22 01:06 anthonykrivonos

@anthonykrivonos I was losing my mind for almost a day, cuz I wasn't able to believe that nobody faces that issue with the package that has 50k downloads weekly...

Myrfion avatar Jun 30 '22 01:06 Myrfion

Thanks guys for reporting. I haven't moved any of my phone projects to React 18 yet, but will prioritize looking in to this.

mbrevda avatar Jul 01 '22 10:07 mbrevda

I set up a branch that, among other things, moves to react 18 but am not seeing this. Did I miss something?

Can you please link to a codesandbox illustrating the issue? Alternatively, try cloning that branch, run npm run dev, and visit http://localhost:3888/ to run the test suit.

mbrevda avatar Jul 03 '22 16:07 mbrevda

Also suffering the same issue, but haven't managed to build a small reproducible sample yet. Seems to occur on re-renders of the tree which it is a part of

The infinite loop it gets stuck in is in react-dom:

function basicStateReducer(state, action) {
  // $FlowFixMe: Flow doesn't like mixed types
  return typeof action === 'function' ? action(state) : action;
}

mgoodfellow avatar Jul 12 '22 13:07 mgoodfellow

Is there any other alternate solution for this ? All i wanted to do was render a fallback element on image error :(

shardbearer-shri avatar Jul 21 '22 08:07 shardbearer-shri

Well, I made a quick component which is drop in compatible just for fallback images when src is an array, and I always know it will have a length of 2 - it's hacky but we really just needed to quickly unblock ourselves and also prove that it was only react-image which was causing issues (in our case that was the case)

import isArray from 'lodash/isArray';
import React, { useEffect, useRef, useState } from 'react';

type ImgProps = {
    src: string | string[];
    alt?: string;
    style?: any;
    srcSet?: string;
};

const HtmlImgFallback = ({ src, ...rest }: ImgProps) => {
    const [imgSrc, setImgSrc] = useState<string>(isArray(src) ? src[0] : src);
    const imgRef = useRef<HTMLImageElement>();

    useEffect(() => {
        setImgSrc(isArray(src) ? src[0] : src);
    }, [src]);

    return (
        <img
            {...rest}
            ref={imgRef}
            src={imgSrc}
            onLoad={() => {
                if (imgRef.current?.clientWidth === 0) {
                    // Broken image
                    setImgSrc(isArray(src) ? src[1] : src);
                }
            }}
            onError={() => {
                setImgSrc(isArray(src) ? src[1] : src);
            }}
        />
    );
};

export default HtmlImgFallback;

This doesn't fit your use case, but I'm sure you can quickly adapt it to render out a fallback component you passed in on error.

Having swapped our usage out we haven't seen any more issues - we weren't using the advanced features of react-image, so it made sense for us to use this as a stop gap as we have been forced to upgrade to React 18.

mgoodfellow avatar Jul 21 '22 09:07 mgoodfellow

We're seeing this as well, but only when useSuspense: false which we need.

Ah, but even without disable suspense, it console.errors:

Can't perform a React state update on a component that hasn't mounted yet. This indicates that you have a side-effect in your render function that asynchronously later calls tries to update the component. Move this work to useEffect instead.

martdavidson avatar Jul 28 '22 18:07 martdavidson

My app freezes when using the onClick prop and then clicking the image

clueking1 avatar Aug 12 '22 20:08 clueking1

@mbrevda I have created reproduction of the issue on CodeSandbox

idChef avatar Aug 31 '22 15:08 idChef

Oh wow, thanks! That is sooo strange. I'll try to dig into this a bit more, open to ideas!

mbrevda avatar Aug 31 '22 16:08 mbrevda

I also can't get the useImage approach to work Codesandbox reproduction

In my app that I'm trying to implement react-image in I have this error: image

idChef avatar Sep 01 '22 06:09 idChef

update: not 100% clear about what changed in React 18, but seems returning earlier in the hook resolves the issue. Please note, PR still requires lots of work, so the fix is not ready yet.

mbrevda avatar Sep 01 '22 09:09 mbrevda

@mbrevda do you have an estimated timeline for when this fix might be ready? My team has had to come up with a workaround for this problem for one of the key features in our software, which is working now but we are experiencing the same freezing problem as detailed above.

Specifically in our case we change the src passed to the Img component based on the props passed into our viewer. We make an API request to get a different sized image based on what the user's image preference is. When they change the size, we generate a new source URL and update the Img component.

This CodeSandbox shows approximately what our setup is. Although it doesn't appear to actually freeze in this particular case.

RykerMunn avatar Sep 27 '22 18:09 RykerMunn

@mbrevda is this issue fixed yet?

Titan-Codes avatar Nov 04 '22 16:11 Titan-Codes

We're experiencing this as well, after upgrading to React 18. Has this been fixed?

10thfloor avatar Feb 24 '23 22:02 10thfloor

Hi all -

First of all, apologies for the delay in addressing this.

Second, I've published a canary build addressing the issue. I'd appreciate if you could take a moment to confirm that this build fixes the issue for you before I publish. You can install it with npm i [email protected] and re-trying whatever was causing the freeze.

You can also see this working in an updated codesandbox

mbrevda avatar Mar 07 '23 17:03 mbrevda

Was anyone able to test? Would like to release this...

mbrevda avatar Mar 09 '23 06:03 mbrevda

@mbrevda sorry for the delay in getting back to you. My team has deployed version 4.1.0 to our dev environment and it has solved this app freezing in our use case.

Thank you for publishing a fix!

RykerMunn avatar Apr 12 '23 18:04 RykerMunn

Awesome, so glad to hear!

mbrevda avatar Apr 13 '23 09:04 mbrevda