0

In the NextJS tutorial we are shown how to render a list of Batman show names from a map. Fetching Batman Shows

In that tutorial there is this fragment that gets the data from the map:

<ul>
  {props.shows.map(({show}) => (
    <li key={show.id}>
      <Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
        <a>{show.name}</a>
      </Link>
    </li>
  ))}
</ul>

But what if the JSON document is much simpler e.g.

[
 {
  "id":1,
  "text":"first doc",
 },
 {
  "id":2,
  "text":"second doc",
 }
]

How do you prepare the data set (got via the getInitialProps call) and access from the props data?

With this example data the goal would be to build a HTML list of values of "text".

1 Answer 1

1

It wasn't overly obvious to me from the React documents React Lists and Keys. The map function is for any array that needs to be rendered.

Any top level element of a JSON record can be mapped.

  <ul>
  {props.mydata.map(({id, text}) => (
    <li key={id}>
      <Link as={`/p/${id}`} href={`/post?id=${id}`}>
        <a>{text}</a>
      </Link>
    </li>
  ))}
</ul>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.