Reactjs 映射函数为';t返回值
我试图返回存储在数据库中的nextjs组件中的类别。 我正在使用map函数返回,但它不起作用Reactjs 映射函数为';t返回值,reactjs,function,dictionary,return,next.js,Reactjs,Function,Dictionary,Return,Next.js,我试图返回存储在数据库中的nextjs组件中的类别。 我正在使用map函数返回,但它不起作用 const showAllCategories = () =>{ return categories.map((c,i)=>{ <Link key={i} href ={`/categories}`} > <a className="btn btn-outline-primary mr-
const showAllCategories = () =>{
return categories.map((c,i)=>{
<Link key={i} href ={`/categories}`} >
<a className="btn btn-outline-primary mr-1 ml-1 mt-3"> {c.name }</a>
</Link>
})
}
const showallcegories=()=>{
返回类别.map((c,i)=>{
{c.name}
})
}
我试图返回html的节标记内的类别名称
<section>
{/* <p>Show categories and tags</p> */}
<div className="pb-5">
{JSON.stringify(showAllCategories())}
{showAllCategories()}
{showAllTags()}
</div>
</section>
{/*显示类别和标记*/}
{JSON.stringify(showAllCategories())}
{showAllCategories()}
{showAllTags()}
我尝试使用JSON.stringyfy进行调试,但结果显示
[null,null,null,null,null,null,null,null]
const showallcegories=()=>{
const showAllCategories = () => {
return categories.map((c, i)=> {
// you forgot return here
return (
<Link key={i} href ={`/categories}`} >
<a className="btn btn-outline-primary mr-1 ml-1 mt-3"> {c.name }</a>
</Link>
)
})
}
返回类别.map((c,i)=>{
//你忘了回这里了
返回(
);
const showallcogories=()=>{
返回类别.map((c,i)=>{
//你忘了回这里了
返回(
);
您缺少return关键字,应该是这样的:
const showAllCategories = () => {
return categories.map((c,i)=> {
return (<Link key={i} href={`/categories}`} >
<a className="btn btn-outline-primary mr-1 ml-1 mt-3"> {c.name }</a>
</Link>
);
})
}
const showallcegories=()=>{
返回类别.map((c,i)=>{
返回(
{c.name}
);
})
}
您缺少return关键字,应该是这样的:
const showAllCategories = () => {
return categories.map((c,i)=> {
return (<Link key={i} href={`/categories}`} >
<a className="btn btn-outline-primary mr-1 ml-1 mt-3"> {c.name }</a>
</Link>
);
})
}
const showallcegories=()=>{
返回类别.map((c,i)=>{
返回(
{c.name}
);
})
}
正如其他人所说,您缺少return关键字
ES6通过省略左括号{
,您可以在一行中返回内容。我认为这会引起很多人的困惑。举两个例子
categories.map((c,i)=>c.name);
上面的自动返回c.name
categories.map((c,i)=>{return c.name;});
这与此相同,但只要您打开括号{,您就需要在其中返回。正如其他人所说,您缺少return关键字 ES6通过省略左括号
{
,您可以在一行中返回内容。我认为这会引起很多人的困惑。举两个例子
categories.map((c,i)=>c.name);
上面的自动返回c.name
categories.map((c,i)=>{return c.name;});
这与此相同,但只要你打开括号{你就需要一个里面的退货。谢谢你,它现在起作用了,它消除了我的疑虑。谢谢你,它现在起作用了,它也消除了我的疑虑