0

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*/
3
  • This SO thread may be what you are looking for. Commented Oct 27, 2022 at 16:08
  • 1
    you might want to checkout dynamic import which is a syntax for giving a Promise that resolves after importing some dynamic links. The code could be inside of a useEffect or you might use React Query Commented Oct 27, 2022 at 16:08
  • yes @PhilipClark it worked, post that as an answer and i will accept it Commented Oct 27, 2022 at 16:25

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.