我试图链接传递对象,它说的地图不是一个函数。我一路都试过了,但运气不好。你知道我犯了什么错误吗?
输入>const sample = [
{
path: "/website",
par: {
abc: "234",
def: "567",
ghi: "8910"
}
}
];
{sample.length !== 0 ? (
sample.map((sample) => (
<Menu.Item
>
<Link
to={{
pathname: sample.path,
search: createURL(sample)
}}
>
{"Sample"}{" "}
</Link>
</Menu.Item>
Map不是函数错误
您也可以这样做。您可以使用Optional chaining?
检查您的数据数组有关可选链接的更多信息?
另外,您需要验证您的数据是否是Array格式。您可以使用Array.isArray(sample)
进行检查。
import React from "react";
const samples = [
{
path: "/website",
par: {
abc: "234",
def: "567",
ghi: "8910"
}
}
];
const App = () => {
return (
<React.Fragment>
{Array.isArray(samples) && samples?.map(sample => {
return (
<div>
{JSON.stringify(sample)}
</div>
)
})}
</React.Fragment>
);
};
export default App;
try this
{sample && sample.length && (
<>
{sample.map((item, index) => (
<Menu.Item
key={index}
className={"text"}
>
<Link
to={{
pathname: item.path,
search: createURL(item),
}}
>
{"item"}{" "}
</Link>
</Menu.Item>
))}
</>
)}