Join live – Get insights, tips, + Q&A from Sanity developers on our latest releases

Trouble mapping over an array of images in a Next.js starter

3 replies
Last updated: Jun 2, 2020
Mapping Over Array of Image - Hey peeps, I'm using the nextjs starter and I've got a few components I will need to build that must accept multiple image files, for example, grids, image sliders, two column layouts. I'm also trying to adapt the Hero component to accept multiple images that I can cycle through. I've changed the schema to accept an array rather than an object. However I'm having trouble getting this to translate on the front end....

function Hero(props) {
  const { heading, backgroundImage, tagline, ctas, node } = props;


  return (
    <div className={styles.root}>
      <div className={styles.content}>
        <h1 className={styles.title}>{heading}</h1>
        { => (
            <img src={`${urlFor(backgroundImage).width(2000).auto("format").url()}`} />
        <div className={styles.tagline}>{tagline && <SimpleBlockContent blocks={tagline} />}</div>
        {ctas && (
          <div className={styles.ctas}>
            { => (
              <Cta {...cta} key={cta._key} />
Jun 2, 2020, 8:39 AM
1. This is my console output
(2) [{…}, {…}]
0: {_key: "b4579d75774a", _type: "image", asset: {…}}
1: {_key: "e93fe9ec7b24", _type: "image", asset: {…}}
length: 2
__proto__: Array(0)

Jun 2, 2020, 8:40 AM
Hi User, inside the
array that you map, have you tried replacing:
… with this?

This should let
use an array item instead of the entire array, which hopefully resolves the issue.
Jun 2, 2020, 10:48 AM
Great that worked thanks User, I've actually decided to use it as img rather than a background image but same principles, I was just passing the data in incorrectly!
Jun 2, 2020, 4:27 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in