2

Basically, I want to add cropping functionality. If user select a file then, user have choice to crop the image if he/she want. When I preview cropped image.

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app

    import React, { useEffect, useState, useRef } from 'react';
    import ReactCrop from 'react-image-crop';
    import 'react-image-crop/dist/ReactCrop.css';
    
    // import Styles from './Image.module.css';
    
    
    const Image = (props) => {
    
      const [crop, setCrop] = useState({ 
        aspect: 3/4,
        unit: 'px',
        x: 0,
        y: 0,
        width: 500,
        height: 500
      });
      const [file, setFile] = useState(null);
      const [imgPreview, setImgPreview] = useState(null);
      const canvasRef = useRef(null);
    
      const filePicker = (e) => {
        setFile(e.target.files[0]);
      };
    
      function image64toCanvasRef (cnvRef, image64, pixelCrop) {
        const canvas = cnvRef;
        canvas.width = pixelCrop.width;
        canvas.height = pixelCrop.height;
        const ctx = canvas.getContext('2d');
        const image = new Image(); // On this line throwing error
        image.src = image64
        image.onload = () => {
          ctx.drawImage(
            image,
            pixelCrop.x,
            pixelCrop.y,
            pixelCrop.width,
            pixelCrop.height,
            0,
            0,
            pixelCrop.width,
            pixelCrop.height
          )
        }
      }
    
      useEffect(() => {
        if (file) {
          const fileReader = new FileReader();
          fileReader.onload = () => {
            setImgPreview(fileReader.result);
          }
          fileReader.readAsDataURL(file);
        }
      }, [file]);
    
      const handleOnCropChanged = (crop) => {
        // console.log('handleOnCropChanged: ', crop);
        const state = {
          ...crop,
          x: crop.x,
          y: crop.y,
          width: crop.width,
          height: crop.height
        }
        setCrop(state);
      };
    
      const handleOnCropComplete = (crop, pixelCrop) => {
        image64toCanvasRef(canvasRef.current, imgPreview, pixelCrop);
      }
    
      return (
        <div
          style={{
            margin: '10px 28px',
          }}
        >
          {
            imgPreview ? (
              <div>
                <ReactCrop 
                  src={imgPreview}
                  crop={crop}
                  keepSelection
                  locked
                  onChange={(crop) => handleOnCropChanged(crop)}
                  onComplete={handleOnCropComplete}
                  onImageLoaded={handleOnImageLoaded}
                />
              </div>
            ) : (
              <input type='file' onChange={filePicker} />
            )
          }
          <div>
            <canvas
              ref={canvasRef}
            ></canvas>
          </div>
        </div>
      )
    };
    
    export default Image;
vicky kumar
  • 31
  • 1
  • 5
  • Does this answer your question? [Invalid hook call. Hooks can only be called inside of the body of a function component](https://stackoverflow.com/questions/56663785/invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-function-com) – Nik Feb 16 '21 at 14:04
  • 1
    @Nik I don't see how this answer his question. Though it's the same error thrown, it can be sometimes quite deceptive to spot where it's wrong. Can you point out where is the invalid hook call? – buzatto Feb 16 '21 at 15:06
  • @buzatto, I agree. Vicky kumar can you please add complete stacktrace of error that you are getting? – Nik Feb 16 '21 at 15:38
  • const image = new Image(); Error throwing on this line inside image64toCanvasRef function – vicky kumar Feb 17 '21 at 15:52
  • Can you please provide the minimal codesandbox example where this issue can be seen? – Nik Feb 18 '21 at 13:22

0 Answers0