Center an aspect ratio crop
masterTo center a crop with a specific aspect ratio, use the makeAspectCrop and centerCrop helper functions. You should listen to the onLoad event of the image to obtain its natural dimensions before calculating the crop.
// 1. Listen to the load event
<ReactCrop crop={crop} aspect={16 / 9}>
<img src={src} onLoad={onImageLoad} />
</ReactCrop>
// 2. Use helpers to calculate the centered crop
function onImageLoad(e) {
const { naturalWidth: width, naturalHeight: height } = e.currentTarget
const crop = centerCrop(
makeAspectCrop(
{
unit: '%',
width: 90,
},
16 / 9,
width,
height
),
width,
height
)
setCrop(crop)
}