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

nue99wik  于 2023-02-12  发布在  React
关注(0)|答案(2)|浏览(100)

我有一个基本的例子,我可以通过下标看到每个菜单项,但是.map格式失败了。下面是代码,我用PDF创建这个来帮助自己学习React. js,但是PDF不清楚,我可以附上PDF吗?
这是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>
 )
 }

这是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

这是菜单应用程序. 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

这是菜单项. 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>
 );
 }
}
ia2d9nvy

ia2d9nvy1#

看起来您正在尝试渲染可变数量的<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} Menu</h1>
        {this.props.menuItems.map((item) => (
          <MenuItem {...item} />
        ))}
      </div>
    );
  }
}
vbkedwbf

vbkedwbf2#

这里至少有两个问题与你可能得到的东西直接相关。
1.检查是否确实将menuItems作为属性传递给Menu组件,如果未定义,则将抛出TypeError: can't access property "map" of undefined
1.仔细查看代码后,我注意到您对this.props.menuItems.map((item) => <p>item</p>)的结果进行了反结构化,这将导致不正确的Map。
根据您的ECMAScript版本,可以按原样替换此行。

{this.props.menuItems?.map((item) => <p>item</p>)}

相关问题