在React页面中使用菜单呈现组件。

huangapple go评论70阅读模式
英文:

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: &#39;Rice&#39;,
  key: &#39;rice&#39;,
},
{
  label: &#39;AB Test&#39;,
  key: &#39;ab&#39;, 
}]

const MainLayout = () =&gt; {
  const {
    token: { colorBgContainer },
  } = theme.useToken();

const navigate = useNavigate();
const onClick = (e)=&gt;{navigate(`/${e.key}`)}

  return (
    &lt;Layout&gt;
         &lt;Sider &gt;
            &lt;Menu
              mode=&quot;inline&quot;
              items={items2}
              onClick = {onClick}

            /&gt;
          &lt;/Sider&gt;
          &lt;Content &gt;

//I Want to open the pages here

          &lt;/Content&gt;
        &lt;/Layout&gt;
      &lt;/Content&gt;

答案1

得分: 1

你需要使用react-router-dom在单击其他MenuItem时进行导航。创建你自己的RouterProvider并将其放入Content中。

&lt;Content&gt;
 &lt;RouterProvider router={router}&gt;
&lt;/Content&gt;

编辑

现在你有两种方式可以导航到你的组件。第一种是使用Link并将其设置为你的标签。

{
  label: &lt;Link title="Rice" to="/rice"&gt;Rice&lt;/Link&gt;,
  key: 'rice',
}

第二种方式是使用hook useNavigate

const navigate = useNavigate();
const onClick = (e) => {navigate(`/${e.key}`)}
//添加到菜单
&lt;Menu
  onClick={onClick}
//其他属性
/&gt;
英文:

You need to use react-router-dom to navigate when you click other MenuItem. Create your own RouterProvider and put it in the Content.

&lt;Content&gt;
 &lt;RouterProvider router={router}&gt;
&lt;/Content&gt;

EDIT

Now you have two way to navigate to your Component. First is using Link and set it to your label.

{
  label: &lt;Link title=&quot;Rice&quot; to=&quot;/rice&quot;&gt;Rice&lt;/Link&gt;,
  key: &#39;rice&#39;, 
}

Second way is hook useNavigate

const navigate = useNavigate();
const onClick = (e)=&gt;{navigate(`/${e.key}`)}
//Add to Menu
&lt;Menu
  onClick={onClick}
//other props
/&gt;

答案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}) =&gt; {
  const {
    token: { colorBgContainer },
  } = theme.useToken();

  const navigate = useNavigate();
  const onClick = (e)=&gt;{navigate(`/${e.key}`)}

  return (
    &lt;Layout&gt;
      &lt;Sider&gt;
        &lt;Menu
          mode=&quot;inline&quot;
          items={items2}
          onClick={onClick}
        /&gt;
      &lt;/Sider&gt;
      &lt;Content&gt;
        {children}
      &lt;/Content&gt;
    &lt;/Layout&gt;
  )
}

In MainLayout.js, you only need to write {children} inside component &lt;Content&gt;, React will do the rest for you to pass the content of Rice or AB or whatever for you. In each page, just place &lt;MainLayout&gt; at the top of the outer of rest of your code.

Please see 2 example files below.


Rice.js:

import MainLayout from &#39;./MainLayout&#39;;

export const Rice = () =&gt; {
  // Do some stuffs here...

  return (
    &lt;MainLayout&gt;
      &lt;div&gt;
        &lt;h2&gt;Best rated rice in the World&lt;/h2&gt;
        &lt;ol&gt;
          &lt;li&gt;Pinipig&lt;/li&gt;
          &lt;li&gt;Riz de Camargue&lt;/li&gt;
          ...
        &lt;/ol&gt;
      &lt;div&gt;
    &lt;/MainLayout&gt;
  )
}

Corn.js:

import MainLayout from &#39;./MainLayout&#39;;

export const Corn = () =&gt; {
  // Do some stuffs here...

  return (
    &lt;MainLayout&gt;
      &lt;div&gt;
        &lt;h2&gt;Best Corn Side Dish Recipes&lt;/h2&gt;
        &lt;ol&gt;
          &lt;li&gt;Whipped-Cream Corn Salad&lt;/li&gt;
          &lt;li&gt;Classic Grilled Corn on the Cob&lt;/li&gt;
          ...
        &lt;/ol&gt;
      &lt;div&gt;
    &lt;/MainLayout&gt;
  )
}

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.

huangapple
  • 本文由 发表于 2023年2月8日 09:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定