GithubHelp home page GithubHelp logo

Comments (10)

adamedmunds avatar adamedmunds commented on May 28, 2024 1

I had the same issue when using version 3.2.0, I resorted to downgrading to version 3.1.0 and panning seems to be working again.

from react-zoom-pan-pinch.

2manyvcos avatar 2manyvcos commented on May 28, 2024 1

@malthew

The following should work if you are not using StrictMode:

const Demo = React.memo(({ imageSrc }) => {
  return (
    <TransformWrapper key={imageSrc}>
      <TransformComponent>
        <ImagePreview src={imageSrc} />
      </TransformComponent>
    </TransformWrapper>
  );
});

// <Demo imageSrc={imageSrc} />

from react-zoom-pan-pinch.

lesjames avatar lesjames commented on May 28, 2024

I'm taking a leap here because I don't know this codebase or the decisions that led to this but it seems like instead of doing this...

useEffect(() => {
  instance.update(props);
  return () => {
    instance.cleanupWindowEvents();
  };
}, [instance, props]);

Cleanup should be in it's own useEffect...

useEffect(() => {
  instance.update(props);
}, [instance, props]);

useEffect(() => {
  return () => {
    instance.cleanupWindowEvents();
  };
}, []);

from react-zoom-pan-pinch.

rjwats avatar rjwats commented on May 28, 2024

This is affecting me also, I haven't found a workaround yet.

from react-zoom-pan-pinch.

2manyvcos avatar 2manyvcos commented on May 28, 2024

Some comments to this issue (for completeness):

Incomplete cleanup

It may be worth noting that the performance fix in 3.1.1 does cleanup mouse and key events, but does not cleanup wheel, double-click and touch events.

StrictMode compatibility

Cleanup should be in it's own useEffect...

useEffect(() => {
  instance.update(props);
}, [instance, props]);

useEffect(() => {
  return () => {
    instance.cleanupWindowEvents();
  };
}, []);

The recommendation from @lesjames fixes the issue in most use cases, but is not a good solution for the future, because it does not work with React StrictMode.

See more details on why that matters. (section "Ensuring reusable state")

Instead, it would be better if the event listeners would be added in the same effect that also cleans them up. Also, instance should be in the effect's dependency array for the same reasons:

useEffect(() => {
  instance.initializeWindowEvents();
  instance.handleInitializeWrapperEvents();

  return () => {
    instance.cleanupWindowEvents();
    // TODO: cleanup rest
  };
}, [instance]);

from react-zoom-pan-pinch.

zzswang avatar zzswang commented on May 28, 2024

Created a Minimal playground before I found this issue.

https://codesandbox.io/s/vigilant-mendeleev-9v7tf4?file=/src/App.js

And a workaround is here if anyone need it:

put following code before <TransformWrapper>

  useEffect(() => {
    setTimeout(() => setImageSrc(0), 300);
  }, [imageSrc]);
  
  return (
           <TransformWrapper key={imageSrc}>
              <TransformComponent>
                <ImagePreview src={imageSrc} />
              </TransformComponent>
            </TransformWrapper>
  )

from react-zoom-pan-pinch.

malthew avatar malthew commented on May 28, 2024

I'm having the same issue and the workaround from @zzswang does not fix it and I imagine it's not a great workaround either for those who have multiple components on one page with multiple useEffects/useStates in each component.

from react-zoom-pan-pinch.

malthew avatar malthew commented on May 28, 2024

@malthew

The following should work if you are not using StrictMode:

const Demo = React.memo(({ imageSrc }) => {
  return (
    <TransformWrapper key={imageSrc}>
      <TransformComponent>
        <ImagePreview src={imageSrc} />
      </TransformComponent>
    </TransformWrapper>
  );
});

// <Demo imageSrc={imageSrc} />

You're right, that did fix it. Totally forgot that React had that feature.

Should probably mention though that according to the React docs, it's not recommended that we rely on React.memo if the code doesn't work without it, as it's only meant to be used as performance optimization.

from react-zoom-pan-pinch.

zzswang avatar zzswang commented on May 28, 2024

@malthew

The rule is to avoid multi rerender happened in a really short time.

from react-zoom-pan-pinch.

piciuok avatar piciuok commented on May 28, 2024

I have same issue. Dynamic content is injected to my image viewer as overlay, and interaction with image hangs.

from react-zoom-pan-pinch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.