Skip to content

How to programatically center an image? #568

Answered by ironexdev
ironexdev asked this question in Q&A
Discussion options

You must be logged in to vote

Thank you, but that leads to flickering in some cases. It looks like the better solution is to use onCropAreaChange event.

const centerFired = useRef(false);
const handleCenterX = () => {
  centerFired.current = true
  setCrop((prev) => ({ ...prev, x: 0 }));
};

<Cropper
  onCropAreaChange={(_, croppedAreaPixels) => {
  if(centerFired.current){
    setCroppedAreaPixels(croppedAreaPixels)
    centerFired.current = false
  }
}}
/>

// Edit changed centerFired from useState to useRef (no need to trigger re-render)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ironexdev
Comment options

Answer selected by ironexdev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants