Comments (9)
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.
Confirm this problem still exists when calling setState in onComplete.
from react-image-crop.
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.
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.
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.
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.
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.
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.
I can also confirm I can replicate this bug too so I'll look into it
from react-image-crop.
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)
- ログイン、新規ユーザー作成処理の実装
- when we have circularCrop as prop, how to set the points on circle? HOT 3
- Need to rotate selected(croped) image HOT 4
- Synchronization Issue Between Multiple Cropper Instances on the Same Page HOT 2
- Image Cropping with 8/3 Aspect Ratio Causing Loss of Half Portion After Retrieval for s3 HOT 2
- Canvas toBlob crop not working on Sandbox (iOS) HOT 5
- Crop mask stays present with zero-size crop HOT 1
- SecurityError: OffscreenCanvas.convertToBlob: Cannot get blob from write-only canvas. HOT 1
- Canvas returns null after cropping image iOS HOT 1
- Mask is does'nt showing the first time HOT 6
- Memory leak risk due to event listeners not removed on componentWillUnmount HOT 1
- mask does not fully cover the image when the image to a smaller aspect ratio HOT 2
- Multiple crop selection on an image at the same time HOT 1
- Can the cropper be used for outpainting?
- React Crop as Dialog Box
- The previous image layout still be kept after switch image
- Feature request: Add the option to rotate the crop selection
- Proportional Stretching of Crop Box
- iPhone 15's 24 MP (res 4284 X 5712) image resolution is making the image black after cropping. HOT 1
- Library not supporting image cropping in iOS WebView HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-image-crop.