How to use variables inside require()?

I’m having a problem here and I don’t know if it’s possible to do this, I’m pulling data as parameters of my route:

onPress={() =>
  navigation.navigate('product', {
    pathToImage: "../../images/airdots_category.jpg",
 })

Then, I get them on my other page.

const pathToImage = route.params.pathToImage;

Now, I want to pass the pathToImage into the require():

<Image source={pathToImage} />

But it does not accept, since the path comes correctly, I find it strange because I just put the path inside a variable.

If you have another way of doing it … please!

1 thought on “How to use variables inside require()?”

  1. You can add require with the image path like this from where you passing your parmas.

    onPress={() =>
      navigation.navigate('product', {
        pathToImage: require("../../images/airdots_category.jpg"),
     })
    
    const pathToImage = route.params.pathToImage;
    
    <Image source={pathToImage} />
    
    Reply

Leave a Comment