如何修复此问题以显示可变数量的菜单项?

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

How do I fix this to show a variable number of menuitems?

问题

以下是您提供的代码的翻译部分:

This is menu.js

import React from "react";
import MenuItem from "../MenuItem";
export default class Menu extends React.Component {
  render() {
    return (
      <div>
        <h1>{this.props.menuName} 菜单</h1>
        <MenuItem {...this.props.menuItems[0]}></MenuItem>
        <MenuItem {...this.props.menuItems[1]}></MenuItem>
        <MenuItem>
          {this.props.menuItems.map((item) => <p>{item}</p>)}
        </MenuItem>
      </div>
    )
  }
}

This is app.js

import "./App.css";
import MenuApp from "./components/MenuApp";

function App() {
  let menuData = [
    {
      menuName: "晚餐",
      menuItems: [
        {
          itemId: 1,
          itemPrice: "12",
          itemName: "烤宽面条",
          itemDescription: "肉和奶酪层叠在自制宽面条之间,加入青椒和洋葱。",
        },
        {
          itemId: 2,
          itemPrice: "10",
          itemName: "芝士馄饨",
          itemDescription: "芝士馅的馄饨,搭配自制红酱。",
        },
        {
          itemId: 3,
          itemPrice: "14",
          itemName: "意大利炸鸡",
          itemDescription: "裹着意大利面酱和大量奶酪的面包屑炸鸡,配自制意大利面。",
        },
      ]
    }
  ];

  return (
    <div className="App">
      <MenuApp data={menuData}></MenuApp>
    </div>
  )
}

export default App

This is MenuApp.js

import "../../App.css";
import Menu from "../Menu";

function MenuApp() {
  return (
    <div className="App">
      <Menu
        menuName="晚餐"
        menuItems={[
          {
            itemId: 1,
            itemPrice: "12",
            itemName: "烤宽面条",
            itemDescription: "肉和奶酪层叠在自制宽面条之间,加入青椒和洋葱。",
          },
          {
            itemId: 2,
            itemPrice: "10",
            itemName: "芝士馄饨",
            itemDescription: "芝士馅的馄饨,搭配自制红酱。",
          },
          {
            itemId: 3,
            itemPrice: "14",
            itemName: "意大利炸鸡",
            itemDescription: "裹着意大利面酱和大量奶酪的面包屑炸鸡,配自制意大利面。",
          }
        ]}
      ></Menu>
    </div>
  )
}

export default MenuApp

This is MenuItem.js

import React from "react";

export default class MenuItem extends React.Component {
  render() {
    return (
      <div>
        <span>${this.props.itemPrice}</span>
        <h2>{this.props.itemName}</h2>
        <p>{this.props.itemDescription}</p>
        <button>Add to Cart</button>
      </div>
    );
  }
}

请注意,您提供的代码中包含一些 HTML 实体编码(如 &quot;&lt;),这些在翻译时已被还原为相应的字符。

英文:

I have this basic example, and I can see each menuitem by subscript, but the .map formation is failing. The following is the code. I am using a PDF to create this to help myself learn React.js, but the PDF is unclear. Can I attach the PDF?

This is menu.js

import React from &quot;react&quot;
import MenuItem from &quot;../MenuItem&quot;
export default class Menu extends React.Component {
 render() {
 return (
 &lt;div&gt;&lt;h1&gt;{this.props.menuName} Menu&lt;/h1&gt;
 &lt;MenuItem {...this.props.menuItems[0]}&gt;&lt;/MenuItem&gt;
 &lt;MenuItem {...this.props.menuItems[1]}&gt;&lt;/MenuItem&gt;
 &lt;MenuItem {...this.props.menuItems.map((item) =&gt; &lt;p&gt;item&lt;/p&gt;)}&gt;&lt;/MenuItem&gt;
 &lt;/div&gt;
 )
 }

This is app.js

import &quot;./App.css&quot;
import MenuApp from &quot;./components/MenuApp&quot;
// let data = {...} (include the data array from above).
function App() {
	let menuData = [
    {
      menuName: &quot;Dinner&quot;,
      menuItems: [
        {
          itemId: 1,
          itemPrice: &quot;12&quot;,
          itemName: &quot;Lasagne&quot;,
          itemDescription:
            &quot;Meat and cheese layered between house-made pasta with bell peppers and onions.&quot;,
        },
		        {
          itemId: 2,
          itemPrice: &quot;10&quot;,
          itemName: &quot;Cheese Ravioli&quot;,
          itemDescription: &quot;Cheese-filled ravioli served with house red sauce.&quot;,
        },
        {
          itemId: 3,
          itemPrice: &quot;14&quot;,
          itemName: &quot;Chicken Parmesan&quot;,
          itemDescription:
            &quot;Breaded chicken topped with marinara sauce and lots of cheese served over house made spaghetti.&quot;,
        },
		]
	}]
  return (
   &lt;div className=&quot;App&quot;&gt;
       &lt;MenuApp data={menuData}&gt;&lt;/MenuApp&gt;
    &lt;/div&gt;
  )
}
export default App

This is MenuApp.js

import &quot;../../App.css&quot;
import Menu from &quot;../Menu&quot;
function MenuApp() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;Menu
        menuName=&quot;Dinner&quot;
        menuItems={[
          {
            itemId: 1,
            itemPrice: &quot;12&quot;,
            itemName: &quot;Lasagne&quot;,
            itemDescription:
              &quot;Meat and cheese layered between house-made pasta with bell peppers and onions.&quot;,
          },
		 {
          itemId: 2,
          itemPrice: &quot;10&quot;,
          itemName: &quot;Cheese Ravioli&quot;,
          itemDescription: &quot;Cheese-filled ravioli served with house red sauce.&quot;,
        },
        {
          itemId: 3,
          itemPrice: &quot;14&quot;,
          itemName: &quot;Chicken Parmesan&quot;,
          itemDescription:
            &quot;Breaded chicken topped with marinara sauce and lots of cheese served over house made spaghetti.&quot;,
        },

        ]}
      &gt;&lt;/Menu&gt;
    &lt;/div&gt;
  )
}
export default MenuApp

This is MenuItem.js

import React from &quot;react&quot;
export default class MenuItem extends React.Component {
 render() {
 return (
  &lt;div&gt;
 &lt;span&gt;${this.props.itemPrice}&lt;/span&gt;
 &lt;h2&gt;{this.props.itemName}&lt;/h2&gt;
 &lt;p&gt;{this.props.itemDescription}&lt;/p&gt;
 &lt;button&gt;Add to Cart&lt;/button&gt;
 &lt;/div&gt;
 );
 }
}

答案1

得分: 1

看起来你正在尝试渲染不同数量的<MenuItem />组件,如果是这样的话,请在Menu.js中尝试以下代码。

import React from "react";
import MenuItem from "../MenuItem";

export default class Menu extends React.Component {
  render() {
    return (
      <div>
        <h1>{this.props.menuName} 菜单</h1>
        {this.props.menuItems.map((item) => (
          <MenuItem {...item} />
        )}
      </div>
    );
  }
}
英文:

It seems you are trying to render a variable number of &lt;MenuItem /&gt; components, in which case please try the below in Menu.js.

<!-- language: lang-js -->

import React from &quot;react&quot;;
import MenuItem from &quot;../MenuItem&quot;;

export default class Menu extends React.Component {
  render() {
    return (
      &lt;div&gt;
        &lt;h1&gt;{this.props.menuName} Menu&lt;/h1&gt;
        {this.props.menuItems.map((item) =&gt; (
          &lt;MenuItem {...item} /&gt;
        ))}
      &lt;/div&gt;
    );
  }
}

答案2

得分: 0

以下是您要翻译的内容:

  1. 检查确保您实际将 menuItems 作为属性传递给 Menu 组件,如果未定义,它将引发 TypeError: 无法访问未定义的属性 "map"
  2. 看了代码两次,我注意到您正在解构 this.props.menuItems.map((item) => &lt;p&gt;item&lt;/p&gt;) 的结果,这会导致错误的映射。

根据您的 ECMAScript 版本,可以将此行替换为以下形式。

{this.props.menuItems?.map((item) => &lt;p&gt;item&lt;/p&gt;)}
英文:

There can be at least two problems here that are directly linked with what you might get.

  1. Check that you are in fact passing menuItems as a property to Menu component, if it is undefined it will throw TypeError: can&#39;t access property &quot;map&quot; of undefined
  2. Looking twice at the code, I noticed that you a de-structuring the result of
    this.props.menuItems.map((item) =&gt; &lt;p&gt;item&lt;/p&gt;) which would result in an incorrect mapping.

Depending on your ECMAScript version, replacing this line as-is would do.

{this.props.menuItems?.map((item) =&gt; &lt;p&gt;item&lt;/p&gt;)}

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

发表评论

匿名网友

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

确定