1

I'd like to set the component's prop when the variable's value defined. Below is my current code.

import Cropper from 'react-easy-crop'
...
interface State {
 ...
  coverFile: File | null;
  ...
}
class Test extends React.PureComponent<Props, State> {
  state = {
    ...
    coverFile: null,
    ...
  };
...
const file = event.target.files;
self.setState({
              coverFile: file[0],
            });

<Cropper
  image={coverFile?coverFile:undefined}
  ...
/>

And this is the error message.

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<CropperProps>): Cropper', gave the following error.
    Type 'null | undefined' is not assignable to type 'string | undefined'.
      Type 'null' is not assignable to type 'string | undefined'.
  Overload 2 of 2, '(props: CropperProps, context?: any): Cropper', gave the following error.
    Type 'null | undefined' is not assignable to type 'string | undefined'.ts(2769)

How can I solve this problem?

5
  • I used react-easy-crop import Cropper from 'react-easy-crop' And this is image type: (JSX attribute) image?: string | undefined Commented May 27, 2021 at 3:42
  • Simply use image={coverFile} Commented May 27, 2021 at 3:50
  • Type 'null' is not assignable to type 'string | undefined' Commented May 27, 2021 at 3:52
  • At least show the assignment of coverFile. That seems to be of importance Commented May 27, 2021 at 3:55
  • I added some code in the question, please have a check and let me know which part of code do you want more. Commented May 27, 2021 at 4:00

3 Answers 3

3

This explains your error message

Type 'null | undefined' is not assignable to type 'string | undefined'.

Cropper's image prop expects a string or undefined but you are passing null or undefined

Did you initialise coverFile as null? If so, you can set it to undefined to silence the error

Update: If you can't figure out where the null is coming from you can simply do this:

<Cropper
  image={coverFile || undefined}
  ...
/>
Sign up to request clarification or add additional context in comments.

5 Comments

The result is same. No overload matches this call. Overload 1 of 2, '(props: Readonly<CropperProps>): Cropper', gave the following error. Type 'null | undefined' is not assignable to type 'string | undefined'. Type 'null' is not assignable to type 'string | undefined'. Overload 2 of 2, '(props: CropperProps, context?: any): Cropper', gave the following error. Type 'null | undefined' is not assignable to type 'string | undefined'.
You need to post more code. How do you assign coverFile? It seems your coverFile is still null
Yes, at first the coverFile value is null. After open the file from the dialog, it will be set. Which part of the code do you want?
Use undefined instead of null
Type 'null' is not assignable to type 'string | undefined'.
0

First, you don't need to use (JSX attribute) image?: string | undefined you can write image?: string because, with strict null checks, an optional parameter automatically adds | undefined

Second, based on react-easy-crop document it needs to image or video (at least one of them) to be required, so if there was an undefined image, you need to pass a valid video parameters I think this could help :

 {coverFile && (
  <Cropper
   image={coverFile}
   ...
 />)
 }
 

1 Comment

Please have a look at @Sohaib's answer. It works.
0

The image props for Cropper stands for string. And it seems to be an error with the code as following that you mentioned.

<Cropper
  image={coverFile?coverFile:undefined}
  ...
/>
It would be fine by replacing it by
  <Cropper
    image={coverFile || undefined}
    ...
  />
or
  {
  image ?
  <Cropper
    image=coverFile
    ...
  /> : ''
  }

Hope your success

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.