Code Monkey home page Code Monkey logo

Comments (9)

sekoyo avatar sekoyo commented on June 6, 2024 1

The problem is that you are calling setState in onComplete, which causes a re-render, and you are statically setting the crop to crop={{height: 100, aspect: 1}}.

Instead you should assign the crop to a variable (or state) e.g.

state = {
  currentCropState: {height: 100, aspect: 1}
};
  return <ReactImageCrop src="/image.png" crop={this.state.currentCropState} ....
onComplete(crop) {
  this.setState({currentCropState: crop});
}

from react-image-crop.

theLordz avatar theLordz commented on June 6, 2024 1

Confirm this problem still exists when calling setState in onComplete.

from react-image-crop.

sekoyo avatar sekoyo commented on June 6, 2024

I can't replicate this, are you sure you're not manipulating the crop in these callbacks? Please post some code or even better a demo

from react-image-crop.

abhidvnt avatar abhidvnt commented on June 6, 2024

I can confirm and reproduce a part of this issue. When onComplete is passed to ReactImageCrop, the cropped are stays fixed according to the initial crop state passed.

class Changer extends React.Component {

    state = {
        currentCropState: {}
    };

    handleOnComplete = (crop) => {
        this.setState({currentCropState: crop});
    }

    render() {
        return <ReactImageCrop src="/image.png" crop={{height: 100, aspect: 1}} onComplete={this.handleOnComplete} />;
    }

}

However, when onComplete={this.handleOnComplete} is not passed, the cropped works as expected.

With onComplete :
with_oncomplete

Without onComplete:
without_oncomplete

The workaround seems to assign a ref to the ReactImageCrop component and access the current crop state using this.refs.{yourAssignedRefName}.state.crop. But beware, accessing it explicitly resets the crop state (after access).

from react-image-crop.

abhidvnt avatar abhidvnt commented on June 6, 2024

I see. It works now.

But, just out of curiosity, since my onComplete handling is not making any change that affects the state of ReactImageCrop, then ReactImageCrop shouldn't be getting re-rendered right?

from react-image-crop.

sekoyo avatar sekoyo commented on June 6, 2024

By default react components will re-render unless something is implemented in shouldComponentUpdate - so fair point, maybe I'll implement something there.

Although a typical shallow compare won't work because your crop object is created on each render, so even though the values are the same crop !== crop.

from react-image-crop.

abhidvnt avatar abhidvnt commented on June 6, 2024

Since change of any property of crop object leads to change in multiple dimensions, it would make sense to check if any property in the new crop property is different from the current crop state. If no property has changed, then perhaps return false from shouldComponentUpdate?

from react-image-crop.

sekoyo avatar sekoyo commented on June 6, 2024

I can also confirm I can replicate this bug too so I'll look into it

from react-image-crop.

sekoyo avatar sekoyo commented on June 6, 2024

What is happening is that you are not setting the crop state, thus when you call this.setState it is triggering a render and ReactCrop is being passed your original crop state instead of the new one.

When using setState in any of the callbacks you must ensure that you also set the new crop state, i.e. this.setState({ something: true. crop })

If your are using outputUnitsInPixels this won't be possible (as you shouldn't pass a pixel crop to ReactCrop). I am going to deprecate outputUnitsInPixels in the next release (1.0.0), the converted pixel crop will always be passed as the last arg in the callbacks.

from react-image-crop.

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.