我查看了很多有類似問(wèn)題的帖子,但我似乎找不到解決方案。這是我的主要 App.jsimport React, { useState, useEffect } from "react";import { weapons } from "./WeaponsList";import DisplayWeapon from "./components/DisplayWeapon";const App = () => { const [items, setItems] = useState([]); useEffect(() => { setItems(weapons); }, []); return ( <div className="container"> <DisplayWeapon items={items} /> </div> );};export default App;這是我要輸出數(shù)組的 DisplayWeapon.jsimport React from "react";const DisplayWeapon = (items) => { console.log(items); return ( <div> {items.map((item) => ( <div>{item.name}</div> ))} </div> );};export default DisplayWeapon;這是我的物品清單export const weapons = [ { name: "Sword", id: 1, }, { name: "Axe", id: 2, }, { name: "Greatsword", id: 3, }, { name: "Hammer", id: 4, },];我已經(jīng)在 App.js 中創(chuàng)建了一個(gè)替代版本,它工作正常,但是一旦我將數(shù)據(jù)傳輸?shù)?DisplayWeapons.js 它告訴我地圖不是一個(gè)函數(shù),當(dāng)我在 DisplayWeapons.js 中控制臺(tái)記錄它時(shí)它告訴我我有一個(gè)數(shù)組。我也試過(guò) Object.keys(),但它只給了我數(shù)組“items”的名稱這是替代版本,它可以工作。import React, { useState, useEffect } from "react";import { weapons } from "./WeaponsList";//import DisplayWeapon from "./components/DisplayWeapon";const App = () => { const [items, setItems] = useState([]); useEffect(() => { setItems(weapons); }); return ( <div className="container"> <div> {items.map((item) => ( <h1> {item.name} {item.id} </h1> ))} </div> </div> );};export default App;
map 不是一個(gè)函數(shù),即使我有一個(gè)數(shù)組
catspeake
2022-12-29 13:45:37