Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass a variable in a file path React JS

So I have this pdf file import link:

import pdf from '../Files/file-1.pdf'

but I want that name of the file "file-1" to be a variable cause its gonna change from one user to another so is it possible to do pass a variable in the path
something like:

const filename = "file-1.pdf"
import pdf from `../File/${filename}` /*ofc this method doesn't 
work for many reasons but I just wanted to explain my question*/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This SO Thread might help.

From Thread:

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

const dynamicFile = require('../somepath/' + someVar);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading