[英]How to render a component that is passed as a prop in React?
我想通过使用循环而不是单独编写每个页面来呈现我的页面。 我制作了这个数组来存储我的页面的信息。
const pages = [
{ title: "Translate", url: "/translate", component: TranslatePage },
{ title: "Study", url: "/study", component: StudyPage },
{ title: "About", url: "/about", component: AboutPage },
{ title: "Home", url: "/", component: HomePage },
{ title: "Terms", url: "/study/terms", component: TermsPage },
];
对于每个页面,我需要以这种格式呈现它:
<Route
path="/study"
exact
element={
<StudyPage
getDocuments={getDocuments}
collectionRef={collectionRef}
docs={docs}
/>
}
/>
如何使用 currentPage.component 渲染每个页面来编写 Route 组件的元素部分? 对于我的页面数组中的每个组件,我需要得到
element={<ComponentReadFromPagesArray
getDocuments={getDocuments}
collectionRef={collectionRef}
docs={docs}
/>}
但我不知道如何从数组中拉出组件,然后将其作为带有道具的自己的组件传递给元素
如果我正确理解你的问题,它会是这样的:
const routes = pages.map(({component: Page, url: path}) => {
const element = (<Page {...{collectionRef, docs, getDocuments}} />);
return (<Route {...{element, exact: true, path}} />);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.