I'm new to React and trying to import some images. However, the image I'm using can be of either jpg
or png
, how do I write code that imports the png if it exists or the jpg
if the png
doesn't exist, all as one constant similar to how the import keyword is used usually?
import Card from "Card.{png, jpg}"
I've tried the following:
let Card = "";
try {
Card=require("path-to-png");
} catch(_) {
try {
Card = require("path-to-jpg")
} catch(__) {
Card = ""
}
}
But it returns the same error:
Module not found: Error: Can't resolve 'path-to-jpg' in '...'