Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(crop-area): initial render with defined objectFit for initialCrop… #551

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cropper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ class Cropper extends React.Component<CropperProps, State> {
classes: { containerClassName, cropAreaClassName, mediaClassName },
} = this.props

const objectFit = this.state.mediaObjectFit
const objectFit = this.state.mediaObjectFit ?? this.getObjectFit()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead add the following code inside the componentDidMount():

const objectFit = this.getObjectFit()
if (objectFit !== this.state.mediaObjectFit) {
  this.setState({ mediaObjectFit: objectFit }, this.computeSizes)
}

I think it would fit better with the current logic of having it stored in the state.

Copy link
Contributor Author

@itsUndefined itsUndefined Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it and I got the following:

Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. 

React limits the number of nested updates to prevent infinite loops.

This probably happened because calling this.computeSizes this early doesn't work. The media must be loaded before this function is called. It calculated a zoom level of NaN and everything broke from there.

I also tried to remove the "this.computeSizes" callback and this cleared the error but the initialCroppedAreaPixels didn't work. I was back to the previous issue.

I don't have time to troubleshoot further.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, let's call this good enough then :)


return (
<div
Expand Down
Loading