next.js Inverse map和use if在map方法中

hm2xizp9  于 2023-04-11  发布在  其他
关注(0)|答案(3)|浏览(116)

在categories.map((category)=〉()中,我需要反转categories.map的元素,并在创建链接之前检查category.isFeatured,但它不允许我进行if语句。

const Header = () => {
  const [categories, setCategories] = useState([])
  useEffect(() => {
    getCategories().then((newCategories) => setCategories(newCategories))
  }, [])
  return (
    <div className="container mx-auto mb-8 px-10">
      <div className="inline-block w-full border-b border-blue-400 py-8">
        <div className="block md:float-left">
          <span className="cursor-pointer text-4xl font-bold text-white">
          </span>
        </div>
        <div className="hidden md:float-left md:contents">
          {categories.map((category) => (
            <Link key={category.slug} href={`/category/${category.slug}`}>
              <span className="mt-2 ml-4 cursor-pointer align-middle font-semibold text-white md:float-right">
                {category.name}
                {console.log(category.name)}
              </span>
            </Link>
          ))}
        </div>
      </div>
    </div>
  )
}
vaj7vani

vaj7vani1#

布拉沃在阵列上是正确的。在评论中反转。
您还需要在.map中添加{},检查category.isFeatured,并删除任何null(否则您将得到空白行)。
例如,下面将颠倒顺序,如果category.isFeatured,则返回链接,否则返回null。最后,它将使用filter(Boolean)删除所有null-一个漂亮的技巧:

import React, { useState, useEffect } from "react";
const Header = () => {
  const [categories, setCategories] = useState([]);
  useEffect(() => {
    getCategories().then((newCategories) => setCategories(newCategories));
  }, []);
  return (
    <div className="container mx-auto mb-8 px-10">
      <div className="inline-block w-full border-b border-blue-400 py-8">
        <div className="block md:float-left">
          <span className="cursor-pointer text-4xl font-bold text-white">
            <Link href={"/"}>CRBStuffReviews</Link>
          </span>
        </div>
        <div className="hidden md:float-left md:contents">
          {categories
            .reverse()
            .map((category) => {
              return category.isFeatured ? (
                <Link key={category.slug} href={`/category/${category.slug}`}>
                  <span className="mt-2 ml-4 cursor-pointer align-middle font-semibold text-white md:float-right">
                    {category.name}
                    {console.log(category.name)}
                  </span>
                </Link>
              ) : null;
            })
            .filter(Boolean)}
        </div>
      </div>
    </div>
  );
};
nxowjjhe

nxowjjhe2#

在map回调函数中,需要使用花括号
{}
在花括号内可以使用IF语句
所以…你的代码将看起来像这样

const Header = () => {
    const [categories, setCategories] = useState([])
      useEffect(() => {
        getCategories().then((newCategories) => setCategories(newCategories))
      }, [])
      return (
        <div className="container mx-auto mb-8 px-10">
          <div className="inline-block w-full border-b border-blue-400 py-8">
            <div className="block md:float-left">
              <span className="cursor-pointer text-4xl font-bold text-white">
                <Link href={'/'}>CRBStuffReviews</Link>
              </span>
            </div>
            <div className="hidden md:float-left md:contents">
              {categories.map((category) => {
               if(category.isFeatured){
               return(
                <Link key={category.slug} href={`/category/${category.slug}`}>
                  <span className="mt-2 ml-4 cursor-pointer align-middle font-semibold text-white md:float-right">
                    {category.name}
                    {console.log(category.name)}
                  </span>
                </Link>
              )}})}
            </div>
          </div>
        </div>
      )
    }
mv1qrgav

mv1qrgav3#

试试看:

<div className="hidden md:float-left md:contents">
        {mrGlobalRequest?.map((category) => {
          if (category.isFeatured) 
           
         
          return (
            <div key={category.slug} href={`/category/${category.slug}`}>
              <span className="mt-2 ml-4 cursor-pointer align-middle font-semibold text-white md:float-right">
                {category.name}
                {console.log(category.name)}
              </span>
            </div>
          );
        })}
      </div>

相关问题