英文:
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 实体编码(如 "
和 <
),这些在翻译时已被还原为相应的字符。
英文:
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 "react"
import MenuItem from "../MenuItem"
export default class Menu extends React.Component {
render() {
return (
<div><h1>{this.props.menuName} Menu</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"
// let data = {...} (include the data array from above).
function App() {
let menuData = [
{
menuName: "Dinner",
menuItems: [
{
itemId: 1,
itemPrice: "12",
itemName: "Lasagne",
itemDescription:
"Meat and cheese layered between house-made pasta with bell peppers and onions.",
},
{
itemId: 2,
itemPrice: "10",
itemName: "Cheese Ravioli",
itemDescription: "Cheese-filled ravioli served with house red sauce.",
},
{
itemId: 3,
itemPrice: "14",
itemName: "Chicken Parmesan",
itemDescription:
"Breaded chicken topped with marinara sauce and lots of cheese served over house made spaghetti.",
},
]
}]
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="Dinner"
menuItems={[
{
itemId: 1,
itemPrice: "12",
itemName: "Lasagne",
itemDescription:
"Meat and cheese layered between house-made pasta with bell peppers and onions.",
},
{
itemId: 2,
itemPrice: "10",
itemName: "Cheese Ravioli",
itemDescription: "Cheese-filled ravioli served with house red sauce.",
},
{
itemId: 3,
itemPrice: "14",
itemName: "Chicken Parmesan",
itemDescription:
"Breaded chicken topped with marinara sauce and lots of cheese served over house made spaghetti.",
},
]}
></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>
);
}
}
答案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 <MenuItem />
components, in which case please try the below in Menu.js
.
<!-- language: lang-js -->
import React from "react";
import MenuItem from "../MenuItem";
export default class Menu extends React.Component {
render() {
return (
<div>
<h1>{this.props.menuName} Menu</h1>
{this.props.menuItems.map((item) => (
<MenuItem {...item} />
))}
</div>
);
}
}
答案2
得分: 0
以下是您要翻译的内容:
- 检查确保您实际将
menuItems
作为属性传递给Menu
组件,如果未定义,它将引发TypeError: 无法访问未定义的属性 "map"
。 - 看了代码两次,我注意到您正在解构
this.props.menuItems.map((item) => <p>item</p>)
的结果,这会导致错误的映射。
根据您的 ECMAScript 版本,可以将此行替换为以下形式。
{this.props.menuItems?.map((item) => <p>item</p>)}
英文:
There can be at least two problems here that are directly linked with what you might get.
- Check that you are in fact passing
menuItems
as a property toMenu
component, if it is undefined it will throwTypeError: can't access property "map" of undefined
- Looking twice at the code, I noticed that you a de-structuring the result of
this.props.menuItems.map((item) => <p>item</p>)
which would result in an incorrect mapping.
Depending on your ECMAScript version, replacing this line as-is would do.
{this.props.menuItems?.map((item) => <p>item</p>)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论