How can I resolve the Error: Element type is invalid?

I am encountering the error message displayed above, and I am uncertain about the cause of this issue. In the code snippet below, I am fetching data from a file named Data.js located in my root folder. When I run the app, I receive the mentioned error message. However, when I use the same data without importing it from an external file, everything works perfectly. This inconsistency is puzzling to me. Can someone please point out what I might be doing wrong? Your assistance is much appreciated. Thanks in advance. Services.js

import { FaCircle, FaShoppingCart, FaLaptop, FaLock } from 'react-icons/fa'
import  { serviceLinks } from '../../../Data'

const data = serviceLinks;
console.log(data);
// const serviceLinks = [
    
//     {
//         title:'Haircut',
//         text:'All dependencies are kept current to keep things fresh.'
       
//     },
//     {
//         title:'Barberos',
//         text:'You can use this design as is, or you can make changes!'
        
//     },
//     {
//         title:'Color',
//         text:'Is it really open source if its not made with love?'
        
//     }, 
// ]

const icons = [
    FaShoppingCart, FaLaptop, FaLock
    
]
function Services () {
    
    return (
        <section className="page-section" id="services">
            <div className="container">
                <div className="text-center">
                    <h2 className="section-heading text-uppercase">Services</h2>
                    <h3 className="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
                </div>
                <div className="row text-center">
                    { data.map((service,idx) => {
                        const Icon = icons[idx];
                        console.log(Icon)
                        return (
                            <div className="col-md-4" key={idx}>                        
                            <span className="fa-stack fa-4x">
                                <FaCircle className="svg-inline--fa fa-circle w-16 fa-stack-2x text-dark" height={100} width={100} />
                                <Icon className="svg-inline--fa fa-shopping-cart fa-w-18 fa-stack-1x fa-inverse" fill="white" />
                            
                            </span>
                            <h4 className="my-3">{service.title}</h4>
                            <p className="text-muted">{service.text}</p>
                        </div>
                        )
                    })
                  
                    }
                </div> 
            </div>
        </section>
    )
}
export default Services;

data.js

 const galleryLinks = [
    {
        title:'HairStyle',
        haircutName:'Afro',
        imgUrl:"assets/img/portfolio/fullsize/dr.cut_thebarbers_afro.jpg",
        imgUrl2:"assets/img/portfolio/thumbnails/dr.cut_thebarbers_afro.jpg",
    },
     {
        title:'Hairstyle',
        haircutName:'Blondo',
        imgUrl:"assets/img/portfolio/fullsize/dr.cut_thebarbers_blondo.jpg",
        imgUrl2:"assets/img/portfolio/thumbnails/dr.cut_thebarbers_blondo.jpg",
    },
    {
        title:'Hairstyle',
        haircutName:'Chica',
        imgUrl:"assets/img/portfolio/fullsize/dr.cut_thebarbers_chica.jpg",
        imgUrl2:"assets/img/portfolio/thumbnails/dr.cut_thebarbers_chica.jpg",
    },
    {
        title:'Hairstyle',
        haircutName:'Nino',
        imgUrl:"assets/img/portfolio/fullsize/dr.cut_thebarbershow_nino.jpg",
        imgUrl2:"assets/img/portfolio/thumbnails/dr.cut_thebarbershow_nino.jpg",
    },
    {
        title:'HairStyle',
        haircutName:'Wavy',
        imgUrl:"assets/img/portfolio/fullsize/dr.cut_thebarbershow_wavy.jpg",
        imgUrl2:"assets/img/portfolio/thumbnails/dr.cut_thebarbershow_wavy.jpg",
    },
    {
        title:'HairStyle',
        haircutName:'Blondo2',
        imgUrl:"assets/img/portfolio/fullsize/dr.cut_thebarbershow_blondo2.jpg",
        imgUrl2:"assets/img/portfolio/thumbnails/dr.cut_thebarbershow_blondo2.jpg",
    },   
    
]

  const serviceLinks = [
    {
        title:'study our themes',
        text:'Our themes are updated regularly to keep them bug free!',
        icons:"assets/img/logo/Hnet.com-image.svg",
    },
    {
        title:'Haircut',
        text:'All dependencies are kept current to keep things fresh.',
        icons:"assets/img/logo/clipper.svg",
    },
    {
        title:'Barberos',
        text:'You can use this design as is, or you can make changes!',
        icons:"assets/img/logo/barber_chair.svg",
    },
    {
        title:'Color',
        text:'Is it really open source if its not made with love?',
        icons:"assets/img/logo/hairstyle.svg",
    }, 
]

export { galleryLinks,  serviceLinks};

index.js

import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.scss'
import Header from './src/components/Header'
import Services from './src/components/Services'

export default function Home() {
  return (
    <div className="container-fluid">
      <Head>
        <title>Dr Cut TheBarber Show</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <main className="main">
       <Header />
       <Services />
      </main>

      <footer className="footer text-center">
        <a
          className="text-decoration-none text-dark"
          href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
          target="_blank"
          rel="noopener noreferrer"
        >
          Powered by{' '}
          <span className={styles.logo}>
            <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
          </span>
        </a>
      </footer>
      </div>
    
  )
}

Answer №1

The issue at hand is not related to how you are importing the Data.js file.

Here is what's causing the error:

const icons = [FaShoppingCart, FaLaptop, FaLock];
{data.map((service, idx) => {
            const Icon = icons[idx];

Essentially, you have an array of 3 icons, but the length of your serviceLinks array in Data.js is 4. This means that at some point in the map function, you are calling icons[3], which results in undefined and triggers the error.

To fix this, simply add one more icon to the icons array to match the length of your last serviceLink.

Check out this Working CodeSandbox for reference.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Mongoose Schema for CSV Import

My current task involves importing a large CSV file into a mongo DB, where the order of the values will determine the key for each entry in the database: Here is an example of the CSV file structure: 9,1557,358,286,Mutantville,4368,2358026,,M,0,0,0,1,0 9 ...

I'm seeking assistance with a frontend script problem. I'm curious if there are alternative approaches to coding this script that may be more effective. Can anyone offer guidance on this?

As a frontend developer specializing in script injection, I have been utilizing Adobe Target to inject scripts. However, this method presents several challenges: 1. It is difficult to debug code errors as my HTML and CSS are wrapped inside ' ' a ...

One way to position a sidebar between two divs is to ensure it adjusts seamlessly to the size of the browser window when resized

In my layout, I have two grids: one for the header and one for the footer. In between these two grids, I am using a sidebar on the left side. Below is the JavaScript code I am using: function adjustSize() { var heights = window.innerHeight; docum ...

Are there any other options besides using the React Material-UI makeStyles() function for styling class Components?

While experimenting with the makeStyles() function in Material-UI's React library, I encountered a specific error message: The use of hooks is limited to the body of a function component. Below is a snippet of the code that triggered this error: ...

A Sweet Alert to Deliver your Morning Toasty Message

I seem to be encountering an issue with my toast message. Instead of the toast popping up immediately after submitting the form, it keeps appearing even if I haven't submitted the form and even when navigating from another page to this one, the toast ...

Dynamic JavaScript Calculations Based on User Input in Real Time

I am in the process of creating a dynamic calculator that updates in real-time based on user input from a form. I have successfully implemented this feature using a sample div, but I am encountering difficulties when attempting to display the total inside ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...

Handling CORS in Vue.js applications

As a newcomer to Vue, I am currently grappling with a CORS issue in my application. During development at http://localhost:8080/, sending a request to , I was able to resolve the CORS problem using the following code snippet: vue.config.js module.exports ...

How can I store a value or text to track view counts or a counter efficiently?

Recently, I've been trying to implement a view counter on my website, similar to what you see on YouTube. Currently, the number of views stands at 23. I managed to find some code that allows me to increment the view count every time I click on an imag ...

Creating a dynamic dropdown menu based on a class value using JavaScript in PHP

There are two dropdown menus on my webpage that display information from my SQL table. The first dropdown contains different "Colors," and the second dropdown contains members associated with each color group. Each member is categorized into different optg ...

What steps can be taken to enhance the functionality of this?

Exploring ways to enhance the functionality of JavaScript using the underscore library. Any ideas on how to elevate this code from imperative to more functional programming? In the provided array, the first value in each pair represents a "bucket" and the ...

The compatibility between Javascript and AJAX functions is currently not functioning as expected

I am attempting to send some data to the server using AJAX with the value obtained from a JavaScript variable. Here is the code snippet: <script type="text/javascript> var url; function applyPhoto(_src) { url = _src; var pho ...

Storing user input in MongoDB after encoding

I am currently exploring the most effective methods for storing and presenting user input in MongoDB. In traditional SQL databases, it is necessary to encode all user input as a precaution against injection attacks. However, in the context of MongoDB, ther ...

What is the best way to compare dates in order to obtain the necessary results?

Question : Filter the JSON array to retrieve specific entries 1: All entries with name "Sam". 2: All entries with date "Dec 2019". // JSON Data provided below. var data = [{ "id":"27", "0":{ "name":"Sam", "date":"2021-02-28" ...

Personalize your Native Base tab design

Recently, I've delved into the world of React Native and now find myself in need of assistance with customizing my tabs. Utilizing advanced tabs by Native Base, I aim to transform this: https://i.stack.imgur.com/xhNwP.png Into something more akin ...

Retrieving the value of an inner div upon clicking the outer div

I currently have a collection of button divs, each containing distinct information: <div class="button"> <div id="first"> Johny </div> <div id="second"> Dog </div> <div id="third"> Pasta & ...

Learn how to utilize a Library such as 'ngx-doc-viewer2' to preview *.docx and *.xlsx files within the application

After 3 days of searching, I finally found a solution to display my *.docx and *.xlxs files in my angular application. The API returns the files as blobs, so my task was to use that blob to show the file rather than just downloading it using window.open(bl ...

Having trouble with playing audio from an array in Javascript

I've been working on creating a Drum Kit website. My approach involves using an array to hold all the sound files, and using a loop to call the play() function. However, I encountered an issue when trying to load the sounds - the debug console showed: ...

Regularly check in with the server via ajax calls

I have a page that needs to send periodic "background" ajax requests after it is loaded. These requests should be sent at specific time intervals. Would using cron be the best option for this task, considering that I have never used it before? Are there a ...

The storage does not reflect updates when using redux-persist with next-redux-wrapper in a Typescript project

I'm currently working on integrating redux-persist with next.js using next-redux-wrapper. However, I'm facing an issue where the storage is not updating and the state is lost during page refresh. Below is my store.ts file: import { createStore, ...