skip to Main Content

enter image description hereI am encountering difficulties applying the key prop to a React component within a mapped array in a Next.js application. The code snippet involves a Slider component and a mapped array of Image components. Despite providing a unique key for each element within the .map() function, the React application does not recognize the key prop, and I’m experiencing unexpected behavior.

"use client";

import Image from "next/image";
import React from "react";
import Slider from "react-slick";
import mediaPlaceHolder from "@public/assets/icons/mediaplaceholder.png";

export default function NetworkSlider({ data }) {
  const settings = {
    dots: false,
    infinite: false,
    speed: 500,
    slidesToShow: 7,
    slidesToScroll: 7,
    initialSlide: 0,
  };

  return (
    <section>
      <Slider {...settings}>
        {data
          .sort((a, b) => {
            return a.display_priority - b.display_priority;
          })
          .slice(0, 21)
          .map((network) => (
            <div key={network.id} className="flex justify-center">
              <Image
                src={
                  network.logo_path !== null
                    ? `http://image.tmdb.org/t/p/original${network.logo_path}`
                    : mediaPlaceHolder
                }
                width={"300"}
                height={"500"}
                className=" rounded-lg h-auto w-auto"
                alt={`Logo of ${network.provider_name} `}
                priority={true}
              />
            </div>
          ))}
      </Slider>
    </section>
  );
}

2

Answers


  1. Judging by your code it’s hard to determine what’s the issue without an example of a single item from the data. To me it seems like you’re trying to access .id incorrectyly – either it doesn’t exist, or nested on a different level of the object.

    Try passing the a different value as a key instead of network.id.
    For example you can try:

    .map((network, index) => ( 
      <div key={index} className="flex justify-center">
        ...
      </div>
    ));
    

    If that helps, try checking where the .id value really is in the data object or whether it’s duplicated.

    Login or Signup to reply.
  2. The common error for this will be that you have a duplicate network id in your array and the key for each item from the mapped array needs to be unique.

    Let’s knock off some possible scenarios.

    1. If the id is present in the array, then there’s another network with an existing id

    2. The id doesn’t exist

    The easiest solution will be to add an array index to the key prop as so:

    
     {data.sort((a, b) => {
                return a.display_priority - b.display_priority;
              })
              .slice(0, 21)
              .map((network, index) => (
                <div key={index} className="flex justify-center">
                  <Image
                    src={
                      network.logo_path !== null
                        ? `http://image.tmdb.org/t/p/original${network.logo_path}`
                        : mediaPlaceHolder
                    }
                    width={"300"}
                    height={"500"}
                    className=" rounded-lg h-auto w-auto"
                    alt={`Logo of ${network.provider_name} `}
                    priority={true}
                  />
                </div>
              ))}
    
    

    But this approach is adviced against

    Instead, you can contact both the id and array index to get a unique key

    
     {data.sort((a, b) => {
                return a.display_priority - b.display_priority;
              })
              .slice(0, 21)
              .map((network, index) => (
                <div key={`${network.id}-${index}`} className="flex justify-center">
                  <Image
                    src={
                      network.logo_path !== null
                        ? `http://image.tmdb.org/t/p/original${network.logo_path}`
                        : mediaPlaceHolder
                    }
                    width={"300"}
                    height={"500"}
                    className=" rounded-lg h-auto w-auto"
                    alt={`Logo of ${network.provider_name} `}
                    priority={true}
                  />
                </div>
              ))}
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search