1

I've got a question about importing a dynamic file path in reactjs. I already know, that I can import a file / image like this:

import pdfImage from '../images/pdf.svg';

Now I wanto to import a file from an object given from the props. I have relative file path in my object saved i.e.

documents/1_1_DEU_DE.pdf

The issue is, that on build, the path to the file looks like this i.e. of the pdf image above.

/static/media/pdf.3fa92281.svg

so I'm searching for something similar to this:

render() {
    import pdfFile from '../' + this.props.termsAndCondition.pdfDocument;
    return (
        <li>
            <a href={pdfFile} target="_blank">
                <img src={pdfImage}></img> 
                {this.props.termsAndCondition.language}
            </a>
        </li>
    );
}

Of course, I know, I can put the files instead of the src folder to the public folder, but is this recommended? I'm sure there is a solution to my problem, but all my researches did not get an answer to me. Thanks in advance.

1
  • Since it’s a static file you may be could put an absolute path including domain http://... Commented Aug 27, 2018 at 15:20

1 Answer 1

2

I've got the answer. I can use require to import files dynamically.

render() {
   let pdfFile = require('../' + this.props.termsAndCondition.pdfDocument);
   return (
      <li>
         <a href={pdfFile} target="_blank" className="link">
            <img src={pdfImage} className="phoneMain"></img> 
            {this.props.termsAndCondition.language}
         </a>
      </li>
   );
}
Sign up to request clarification or add additional context in comments.

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.