简体   繁体   English

如何使用包含一些组件名称的数组来呈现那些预定义的组件

[英]How to use an array containing some component names to render those predefined components

Okay, So I've this question which is not as simple as It sounds.好的,所以我有这个问题并不像听起来那么简单。 I'm designing a web project where I could add my articles.我正在设计一个网络项目,我可以在其中添加我的文章。 Now the thing is that an article would be a big page which would require a lot of formatting and the other things.现在的问题是,一篇文章将是一个大页面,需要大量的格式和其他事情。 Therefore I thought of adding one component per one article that I write.因此,我想为我写的每一篇文章添加一个组件。

eg, Suppose its ArticleX.js例如,假设它的 ArticleX.js

export const ArticleX = () => {
return (
    <div>
        //My article X goes here
    </div>
)}

Similarly suppose like this one I've a lot of other articles too.同样假设像这样我也有很多其他文章。 Let's say, 2 more.比方说,还有2个。 ArticleA, ArticleB. A条,B条。

So far so good.到现在为止还挺好。 Now I've a page called ShowArticles.js which is nothing but another React Component which needs to render all these articles we just talked about.现在我有一个名为 ShowArticles.js 的页面,它只不过是另一个需要渲染我们刚刚讨论过的所有这些文章的 React 组件。

A simple approach to do that would be doing something like this in ShowArticles:一个简单的方法是在 ShowArticles 中做这样的事情:

import {Article X} from './Articles/ArticleX';
import {Article A} from './Articles/ArticleA';
import {Article B} from './Articles/ArticleB';

export const ShowArticles = () => {
    return (
        <div>
            <Article X />
            <Article A />
            <Article B />
        </div>
    )
}

Now the thing is that the articles would go on increasing and each time I'll have to modify the ShowArticles.js file to accommodate the new changes.现在的问题是文章会不断增加,每次我都必须修改 ShowArticles.js 文件以适应新的变化。 Instead I want to make an array of these components, If not possible then the component names and then use it to render them dynamically.相反,我想制作这些组件的数组,如果不可能,则使用组件名称,然后使用它来动态呈现它们。 Think of an array just like some json data being pulled out of a json file.将数组想象为从 json 文件中提取的一些 json 数据。

So Suppose to do that I make an array of Component names.因此,假设我创建了一个组件名称数组。

let travelArticles = [
    {id: 1, articleName: 'ArticleX', source:'./Articles/ArticleX'},
    {id: 2, articleName: 'ArticleA', source:'./Articles/ArticleA'},
    {id: 3, articleName: 'ArticleB', source:'./Articles/ArticleB'}
];

Now the question is, how can I use this array to reframe my ShowArticles.js file so that each of these component name and source is taken and then that information is used to : 1. Import that component from the source 2. Render that component inside ShowArticles.js现在的问题是,我如何使用这个数组来重构我的 ShowArticles.js 文件,以便获取这些组件名称和源中的每一个,然后使用该信息: 1. 从源导入该组件 2. 渲染该组件在 ShowArticles.js 中

export const ShowArticles = ({travelArticles}) => {
    return (
        <div>
            // How to import components using source and name in travelArticles and then use that information to render them ?
        </div>
    )
}

Now If I'm able to do something like this, all I'll need to do is to create the new article and then add it's name and source in the array.现在如果我能够做这样的事情,我需要做的就是创建新文章,然后在数组中添加它的名称和来源。 Rest everything would be taken care by the code itself and also I would be able to use this array to show some featured content on some other page than just ShowArticles.js其余的一切都将由代码本身来处理,而且我还可以使用此数组在其他页面上显示一些特色内容,而不仅仅是 ShowArticles.js

Thanks in advance!提前致谢!

You can use dynamic imports您可以使用动态导入

 let travelArticles = [ {id: 1, articleName: 'ArticleX', source:'./Articles/ArticleX'}, {id: 2, articleName: 'ArticleA', source:'./Articles/ArticleA'}, {id: 3, articleName: 'ArticleB', source:'./Articles/ArticleB'} ]; export const ShowArticles = ({travelArticles}) => { const [components, setComponents] = useState([]); useEffect(() => { const promises = travelArticles.map(art => import(art.source).then(mod => mod.default)); Promise.all(promises).then(comps => setComponents(comps)); }, []); return ( <div> {components.map((Article, index) => <Article {...travelArticles[index]} />)} </div> ) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据这些组件中定义的某些操作在页面上呈现不同的组件? - How do I render different components on a page according to some action defined inside those said components? 等待数组被填充,然后渲染这些组件 - Wait for array to be filled and then render those components 如何使用一个组件来呈现值数组 - How to use one component to render an array of value 如何渲染具有不同组件名称的多个组件? - How to render multiple components with different component name? 如何在两个单独的父组件中重用 Angular 组件(包含表单)以实现不同的自定义类型的用途? - How to re-use an Angular component (containing a form) for different, customized type of uses in two separate parent components? 当多个组件在 React 中使用 state 时,如何仅使用 state 渲染单个组件? - How to only render an individual component with state, when multiple components use that state in React? 在 React 中按名称渲染动态组件 - Render dynamic components by names in React 如何在 React 中使用对象数组渲染组件 - How to render components with array of objects in React 如何在 GET 请求中使用 fetch 并呈现这些结果? - How to use fetch inside GET request and render those result? 如何在单个组件中渲染多个道具,然后将这些道具传递给 React 中的子组件 - How do i render multiple props in a single component and then pass those props to a child component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM