0

A image will load if i type in the path of the image like below:

 <li><a href="#"><img src={require('../images/it.jpg')} />

but if i do it dynamically where it gets the image url from JSON, it just won't work, i get this error Error: Cannot find module "."

const TopMovies = ({tops}) =>(

   <ul>

<li><a href="#"><img src={require(tops.image)} />
</a></li>
    </ul>
    );
};
    export default TopMovies;
Sean
  • 3
  • 1

1 Answers1

0

From the React Native Image Docs (I know your code is not react-native)

In order for this to work, the image name in require has to be known statically.

// GOOD
<Image source={require('./my-icon.png')} />

// BAD
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive';
<Image source={require('./' + icon + '.png')} />

// GOOD
var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png');
<Image source={icon} />
bennygenel
  • 23,896
  • 6
  • 65
  • 78