1

In this project I used NextJS JSX.

I try to use map() function to render the array element into link. In the return the first map() render nothing, but ***the second map() is render out links and show on browser.

This two pieces of map() are same, I have no idea why only the second map() is working.

import Link from 'next/link'
import { Fragment } from 'react'

import Nav from '../components/nav'

const Home = () =>{
  let navList = ["Home", "Project", "blog", "About"]

  return(
    <Fragment>
      {
        navList.map((navItem) => {
          navItem === "Home" ? 
          <Link href="/" key={navItem}><a>{navItem}</a></Link> : 
          <Link href={`../${navItem}`} key={navItem}><a>{navItem}</a></Link>
        })
      }

      {
        navList.map((navItem) => (
          navItem === "Home" ? 
          <Link href="/" key={navItem}><a>{navItem}</a></Link> : 
          <Link href={`../${navItem}`} key={navItem}><a>{navItem}</a></Link>
        ))
      }
    </Fragment>
  )
}

export default Home;
  • 3
    In the first case you need to `return` the JSX because you're using `{}` and the `return` isn't implied whereas in the second example you're using `()` and the `return` _is_ implied. – Andy Sep 25 '21 at 08:11
  • I see the problem, thanks so much for the answer, that is very useful, and next time I'll be more careful with my code ^_^ – wingman Frankie Sep 25 '21 at 08:17

0 Answers0