react-native-image-resizer icon indicating copy to clipboard operation
react-native-image-resizer copied to clipboard

Need to use ImageResizer.default.createResizeImage() instead of what is suggested in docs

Open daku opened this issue 7 years ago • 9 comments

Something's up with the setup. On my setup, Android, Flow, react-native 0.52, I have to do this:

import * as ImageResizer from 'react-native-image-resizer'
...
ImageResizer.default.createResizedImage(imageInfo.name, thumbnailWidth,...)

Anything else, including what's suggested in the docs, results in this error:

TypeError: undefined is not a function (evaluating 'ImageResizer.createResizedImage(imageInfo.name, thumbnailWidth, thumbnailWidth * aspectRatio, imageType, compressionQuality)'

I am not too familiar about this, but there are several flavors of index.js, and 1) I wasn't sure what's getting selected in my case, 2) they export conflicting sets of things. In the flow case the function is a named export and in the typescript case as well as the other files its not. Just pointing this out, FYI, not sure if this has anything to do with this problem.

daku avatar Feb 21 '18 21:02 daku

Have you tried:

import ImageResizer from 'react-native-image-resizer'

?

pstanton avatar May 03 '18 05:05 pstanton

Have you tried:

import ImageResizer from 'react-native-image-resizer'

Then you'll get the error: Module 'react-native-image-resizer' has no default export.

That's simply because the type definition, which is part of the package does not match to the export of the .js file.

jens-duttke avatar Jul 27 '18 09:07 jens-duttke

@jens-duttke works for me

pstanton avatar Jul 28 '18 02:07 pstanton

@pstanton: Make sure to set allowSyntheticDefaultImports to false in your tsconfig.json

jens-duttke avatar Jul 29 '18 20:07 jens-duttke

@jens-duttke

That's simply because the type definition, which is part of the package does not match to the export of the .js file.

I don't really understand what you meant. Do you know how we could fix that ?

PierreCapo avatar Oct 27 '19 09:10 PierreCapo

@PierreCapo

Instead of default exporting an object with properties (where the object is ImageResizer.default then), the export could looks like:

export const createResizedImage = (...) => { ... };

The type definition could looks like:

declare module 'react-native-image-resizer' {
  export interface Response {
    path: string;
    uri: string;
    size?: number;
    name?: string;
  }

  export function createResizedImage(
      uri: string,
      width: number,
      height: number,
      format: 'PNG' | 'JPEG' | 'WEBP',
      quality: number,
      rotation?: number,
      outputPath?: string
    ): Promise<Response>;
}

then.

jens-duttke avatar Oct 29 '19 20:10 jens-duttke

If I understand correctly this should move from export default to named export, right ? Thanks for the answer and code btw

PierreCapo avatar Oct 30 '19 12:10 PierreCapo

Correct

jens-duttke avatar Oct 30 '19 15:10 jens-duttke

any fix for this issue?

// this didn't work
import ImageResizer from 'react-native-image-resizer';
ImageResizer.createResizedImage(...);

// this also didn't work
import * as ImageResizer from 'react-native-image-resizer';
ImageResizer.default.createResizedImage(...);

versions:

typescript: ~3.9.5
react-native: ~0.62.2
react-native-image-resize: ^1.2.6

kbhyan avatar Sep 01 '20 16:09 kbhyan

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 01 '22 19:09 stale[bot]

This issue has been automatically closed. Thank you for your contributions.

stale[bot] avatar Sep 09 '22 04:09 stale[bot]