-1

In my react app, I m trying to import the img source, but getting any error as

TS2322: Type 'Promise<any>' is not assignable to type 'string'. how to fix it or what is the correct way to import an element in variable?

here is my code:

import React, { FC } from "react";
import "./barkingRoad.scss";



const BarkingRoad: FC = () => {

const slidex = import("./../../../../assets/images/small/boomerang-site1.jpg");

return (
    <div className="boomerang-slider-barkingRoad">
    
        <div className="project-data">
            <div className="boomerang-site-slide">
                <div className="prev"><a href="#">Previus</a> </div>
                <div>
                    <img src={slidex} /> //getting error
                </div>
                <div className="next"><a href="#">Next</a> </div>
                {/* srcSet={`${slide} 360w, ${slide} 768w, ${slide} 1366w  `} */}
            </div>
        </div>

    </div>
)

}

export default BarkingRoad;

This is works:

const slidex = require("./../../../../assets/images/small/boomerang-site1.jpg");

but getting an error as ;

 8:20  error  Require statement not part of import statement 
user2024080
  • 1
  • 14
  • 56
  • 96

4 Answers4

1

I suggest two options.

  1. Import outside the component like this.

const slidex = require("...");

const BarkingRoad: FC = () => {
...
}
  1. Use await keyword.

     const BarkingRoad: FC = async () => {
     ...
     const slidex = await require...
     }
    

If you defined the image in the public directory, you can use the url, instead of importing in the code.

Chuck
  • 776
  • 5
  • 18
  • Cannot find name 'async'. - error raising – user2024080 Sep 09 '21 at 09:56
  • `Type '() => Promise' is not assignable to type 'FC<{}>'. Type 'Promise' is missing the following properties from type 'ReactElement': type, props, keyts(2322) const BarkingRoad: React.FC<{}>` – user2024080 Sep 09 '21 at 10:02
  • Seems like you are using `typescript`. Take a look at this one to get to know how to define `async` function in typescript. https://stackoverflow.com/questions/38744159/in-typescript-how-to-define-type-of-async-function – Chuck Sep 09 '21 at 10:04
  • Yes, I agree. But my case i am not able to get the correct way of declaration, can you help me? – user2024080 Sep 09 '21 at 10:10
0

const slidex = import is dynamic import so retuning a promise<any> and src recieves a direct url string

Try to set it directly:

<img src="./../../../../assets/images/small/boomerang-site1.jpg" />
Ryan Le
  • 7,708
  • 1
  • 13
  • 23
0

Try importing like this:

import slidex from "./../../../../assets/images/small/boomerang-site1.jpg";
Harshit Rastogi
  • 1,996
  • 1
  • 10
  • 19
0

The import function returns a Promise which is not a string.

Your could use the following solutions:

 const BarkingRoad: FC = async () => {   
 ...
 const slidex = await import('...');
 ...
Alex Fischer
  • 193
  • 1
  • 2
  • 12
  • `'await' expressions are only allowed within async functions and at the top levels of modules.` when i use `const slidex = await import('...');` – user2024080 Sep 09 '21 at 09:46
  • As siruis suggested you also have to add the async keyword. [here](https://stackoverflow.com/a/69115984/9415076) – Alex Fischer Sep 09 '21 at 09:52