最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Create a loop to import files dynamically in React - Stack Overflow

matteradmin4PV0评论

Consider I have a folder with .md files like this:

/static/blog/
  example_title.md
  another_example.md
  three_examples.md

and I have an array including all titles:

const blogEntries = ["example_title", "another_example", "three_examples"]

in my ponent I want to import all of them, notice that there could be fewer or more.

Is there any way to dynamically import them similar to:

for (let entry in blogEntries) {
  import `Markdown_${entry}` from `/static/blog/${entry}.md`
}

and later in my render I can do:

<Markdown_three_examples />

the above obviously would not work, but I'm looking for a way to achieve similar.

Consider I have a folder with .md files like this:

/static/blog/
  example_title.md
  another_example.md
  three_examples.md

and I have an array including all titles:

const blogEntries = ["example_title", "another_example", "three_examples"]

in my ponent I want to import all of them, notice that there could be fewer or more.

Is there any way to dynamically import them similar to:

for (let entry in blogEntries) {
  import `Markdown_${entry}` from `/static/blog/${entry}.md`
}

and later in my render I can do:

<Markdown_three_examples />

the above obviously would not work, but I'm looking for a way to achieve similar.

Share asked Oct 23, 2018 at 19:23 supersizesupersize 14.9k19 gold badges85 silver badges144 bronze badges 4
  • When you build a react app, all your import / export stuff is converted into one big js file. There's no way to import stuff dynamically because babel can't build that into its file. The solution is to build a Markdown ponent and fetch() the md file from the server when it has mounted. – user5734311 Commented Oct 23, 2018 at 19:30
  • @ChrisG can you give an example as answer? – supersize Commented Oct 23, 2018 at 19:39
  • What part do you need help with? In ponentDidMount(), use fetch() to load the Markdown file. Set its content as part of your state so it gets rendered. Implement the loop by rendering multiple ponents, using your file array and map(). – user5734311 Commented Oct 23, 2018 at 20:02
  • Two people have now suggested using import(), which is not required to do this at all, and imo takes the question strictly at face value while ignoring the bigger picture. Here's my take: codesandbox.io/s/l2jlq3j78m – user5734311 Commented Oct 23, 2018 at 20:20
Add a ment  | 

3 Answers 3

Reset to default 4

You can try to use dynamic imports - import(). As the name implies, they work during runtime, and can have dynamic urls.

Note: currently they are supported natively by Chrome and Safari, but not by firefox, IE, and Edge. However, you can use webpack to solve that.

const blogEntries = ["example_title", "another_example", "three_examples"]

const loadEntries = (entries) =>
  Promise.all(entries.map((entry) => import(`/static/blog/${entry}.md`)));

loadEntries(blogEntries)
  .then((entries) => console.log(entries));
const Markdown = {}

for (let entry in blogEntries) {
  Markdown[entry] = require(`/static/blog/${entry}.md`)
}

Take a look at the following link. It's essentially used the external library - marked, fetching the content, then rendering the content to the screen. Essentially, you can call multiple fetches for each file needed and render the content dynamically.

How do I load a markdown file into a react ponent?

Note: There is a proposal in the ECMAScript standard for a dynamic import. You can read more about this here: https://developers.google./web/updates/2017/11/dynamic-import

Post a comment

comment list (0)

  1. No comments so far