1

To be clear:

<img src={require('../styles/myimage.jpg')} />

works fine. But I want to get this path dynamically as a string from api. In my parent component I fetch it and pass to child:

<img src={require(this.props.image)} />

In this case this.props.image is the same as '../styles/myimage.jpg'. But it does not work for me and throw this error "Uncaught (in promise) Error: Cannot find module "."

How can I fix it?

The directory tree is (I am trying to fetch myimage.jpg from image.js)enter image description here: And webpack config: enter image description here

2
  • Make sure that the props have a value when your render is called Commented Jul 18, 2017 at 10:01
  • @ShubhamKhatri I even tried conditional rendering, so I make request with fetch...setState({image: data, loadImage: true}) and then in my child component i check if loadImage is true then I render "this.props.loadImage ? <img src={require(this.props.image)} /> : null". Commented Jul 18, 2017 at 10:14

2 Answers 2

1

Do a simple test, move styles path with contains yours jpg files to same path of your component and try this path in props:

props.image = './styles/myimage.jpg'

then

<img src={require(this.props.image)} />
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the comment what you can do is in your render method try this :

if (!this.props.image) return false;

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.