英文:
rendering a component inside a react page using Menu
问题
我有一个菜单和页面的主体。我已经创建了不同的页面作为组件,并添加了一些文本。我希望的是,当我点击侧边栏菜单时,这些组件会在主页面中显示。如何使这个工作?
const items2 = [{
label: 'Rice',
key: 'rice',
},
{
label: 'AB Test',
key: 'ab',
}]
const MainLayout = () => {
const {
token: { colorBgContainer },
} = theme.useToken();
const navigate = useNavigate();
const onClick = (e) => {navigate(`/${e.key}`)}
return (
<Layout>
<Sider>
<Menu
mode="inline"
items={items2}
onClick={onClick}
/>
</Sider>
<Content>
{/*在这里打开页面*/}
</Content>
</Layout>
);
};
英文:
I have a menu and the main body of the page. I have created different pages as components with some text. All I want is that when I click on the side bar menu, the components are displayed in the main page. How can I make this work?
const items2 = [{
label: 'Rice',
key: 'rice',
},
{
label: 'AB Test',
key: 'ab',
}]
const MainLayout = () => {
const {
token: { colorBgContainer },
} = theme.useToken();
const navigate = useNavigate();
const onClick = (e)=>{navigate(`/${e.key}`)}
return (
<Layout>
<Sider >
<Menu
mode="inline"
items={items2}
onClick = {onClick}
/>
</Sider>
<Content >
//I Want to open the pages here
</Content>
</Layout>
</Content>
答案1
得分: 1
你需要使用react-router-dom在单击其他MenuItem时进行导航。创建你自己的RouterProvider并将其放入Content中。
<Content>
<RouterProvider router={router}>
</Content>
编辑
现在你有两种方式可以导航到你的组件。第一种是使用Link并将其设置为你的标签。
{
label: <Link title="Rice" to="/rice">Rice</Link>,
key: 'rice',
}
第二种方式是使用hook useNavigate
const navigate = useNavigate();
const onClick = (e) => {navigate(`/${e.key}`)}
//添加到菜单
<Menu
onClick={onClick}
//其他属性
/>
英文:
You need to use react-router-dom to navigate when you click other MenuItem. Create your own RouterProvider and put it in the Content.
<Content>
<RouterProvider router={router}>
</Content>
EDIT
Now you have two way to navigate to your Component. First is using Link and set it to your label.
{
label: <Link title="Rice" to="/rice">Rice</Link>,
key: 'rice',
}
Second way is hook useNavigate
const navigate = useNavigate();
const onClick = (e)=>{navigate(`/${e.key}`)}
//Add to Menu
<Menu
onClick={onClick}
//other props
/>
答案2
得分: 1
在React中,要在一个组件内部渲染另一个组件,React提供了一个特殊的props名称children
。
要实现你的需求,你可以像这样做:
MainLayout.js:
export const MainLayout = ({children}) => {
const {
token: { colorBgContainer },
} = theme.useToken();
const navigate = useNavigate();
const onClick = (e)=>{navigate(`/${e.key}`)}
return (
<Layout>
<Sider>
<Menu
mode="inline"
items={items2}
onClick={onClick}
/>
</Sider>
<Content>
{children}
</Content>
</Layout>
)
}
在MainLayout.js
中,你只需要在<Content>
组件内部写{children}
,React会为你做剩下的事情,将Rice或AB或其他内容传递给你。在每个页面中,只需将<MainLayout>
放在你的代码的外部顶部。
请看下面的两个示例文件。
Rice.js:
import MainLayout from './MainLayout';
export const Rice = () => {
// 在这里做一些事情...
return (
<MainLayout>
<div>
<h2>世界上最好吃的大米</h2>
<ol>
<li>Pinipig</li>
<li>Riz de Camargue</li>
...
</ol>
<div>
</MainLayout>
)
}
Corn.js:
import MainLayout from './MainLayout';
export const Corn = () => {
// 在这里做一些事情...
return (
<MainLayout>
<div>
<h2>最佳玉米配菜食谱</h2>
<ol>
<li>淡奶油玉米沙拉</li>
<li>经典烤玉米棒</li>
...
</ol>
</div>
</MainLayout>
)
}
你可以在React官方文档中阅读更多并尝试这个示例代码。
这是React的基本概念,所以在你开始构建大型项目之前,我建议你先阅读这些文档,或者找一些针对初学者的React教程系列,它们会解释React的关键概念,这样你就能节省更多时间。
英文:
To render a component inside other component, React provides a special props name children
.
To achieve your requirement, you can do like this:
MainLayout.js:
export const MainLayout = ({children}) => {
const {
token: { colorBgContainer },
} = theme.useToken();
const navigate = useNavigate();
const onClick = (e)=>{navigate(`/${e.key}`)}
return (
<Layout>
<Sider>
<Menu
mode="inline"
items={items2}
onClick={onClick}
/>
</Sider>
<Content>
{children}
</Content>
</Layout>
)
}
In MainLayout.js
, you only need to write {children}
inside component <Content>
, React will do the rest for you to pass the content of Rice or AB or whatever for you. In each page, just place <MainLayout>
at the top of the outer of rest of your code.
Please see 2 example files below.
Rice.js:
import MainLayout from './MainLayout';
export const Rice = () => {
// Do some stuffs here...
return (
<MainLayout>
<div>
<h2>Best rated rice in the World</h2>
<ol>
<li>Pinipig</li>
<li>Riz de Camargue</li>
...
</ol>
<div>
</MainLayout>
)
}
Corn.js:
import MainLayout from './MainLayout';
export const Corn = () => {
// Do some stuffs here...
return (
<MainLayout>
<div>
<h2>Best Corn Side Dish Recipes</h2>
<ol>
<li>Whipped-Cream Corn Salad</li>
<li>Classic Grilled Corn on the Cob</li>
...
</ol>
<div>
</MainLayout>
)
}
You can read more and play around with the example code from React's official documents.
It is the basic concept of React, so before you start to build something big, I suggest to follow this docs first or find some series of React tutorials for beginner, they will explain key concepts of React so you would not save more time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论