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

Conversation

itsUndefined
Copy link
Contributor

Fixes: #550

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@itsUndefined
Copy link
Contributor Author

I also confirmed that this change doesn't affect the fix for #506 so it's safe

Copy link
Owner

@ValentinH ValentinH left a comment

Choose a reason for hiding this comment

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

Thank you for reporting the issue and providing a fix. I have one suggestion though 🙂

@@ -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 :)

@ValentinH ValentinH added the patch Increment the patch version when merged label Jul 26, 2024
@ValentinH ValentinH merged commit 4f27fdf into ValentinH:main Jul 26, 2024
4 checks passed
Copy link

🚀 PR was released in v5.0.8 🚀

@github-actions github-actions bot added the released This issue/pull request has been released. label Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Increment the patch version when merged released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InitialCroppedAreaPixels no longer works properly after v5 upgrade
2 participants