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

https://i.sstatic.net/9PehR.jpgI 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

I'm curious about the meaning of "!" in pseudo-code. I understand that it represents factorial, but I'm unsure of how to properly interpret it

Can someone explain the meaning of ! in pseudo-code to me? I understand that it represents factorial, but for some reason I am having trouble translating it. For example: I came across this code snippet: if (operation!= ’B’ OR operation != ’D’ O ...

Certain components in Next.js are not properly reflecting the styles defined in Tailwind CSS

After successfully building a single-page portfolio site on Next using Tailwind, I decided to expand by adding a second page to my project. I created a new file in the pages directory and referenced a new component. While the content is accessible via the ...

Leveraging Express request-specific variables to facilitate logging with correlation IDs

Our node express app is now incorporating correlation id's to link requests together. These id's are sent in the header from other services, and our middleware captures them, creates a bunyan logger with the id, and includes it in the request obj ...

Efficient Ways to Store Text and Images in Vue.js

I have developed a tool that enables users to upload an image and overlay custom text on it, which can be viewed and edited in the browser. My query is whether it is feasible to save the combined image and text as separate files (e.g., jpg/png) using VueJS ...

create a sapper route using JSON data

Beginning with the official Sapper template, I am keen on implementing export default as recommended by eslint: export default function get(_, res) { res.writeHead(200, { 'Content-Type': 'application/json', }); res.end(conte ...

Steps to configure npm start for an electron application using "babel-node --presets es2015,stage-3"

I've been experimenting with getting my npm start to work properly for electron. Typically, you would start a non-distributed app with electron . or ./node_modules/.bin/electron .. However, due to my use of NodeJS v8.4.0 and ES6/7 syntax, my npm start ...

Encountering difficulties in accessing state while utilizing async callback functions in React.js

After finding this function on Stack Overflow, I decided to use it in order to update a database and profile information immediately after a user signs up. The function is working as expected, but I am encountering an issue where I can no longer access the ...

Tips for achieving printing with the "Fit sheet on one page" option in JavaScript

Is there a way to print a large table of data with 200 or 300 rows all on one page, similar to the Online MS Excel print option 'Fit Sheet on One Page'? I attempted the code below: var tble = document.createElement("table"); tble.id = "tble"; d ...

Capture data from ajax effectively by extracting and executing manipulations seamlessly

I have a project where I need to retrieve images from a database using Ajax and display them using a carousel plugin. This is the process: An image URL is saved to the database by an admin The frontend script makes an Ajax call to a PHP file and retrieve ...

Create a wait function that utilizes the promise method

I need to wait for the constructor() function, which contains an asynchronous method handled by Promise. My goal is to wait for two asynchronous methods within the constructor, and then wait for the constructor itself. However, my code is throwing an err ...

The system encountered an error stating that the required component "Footer" in the main directory was not located in the specified node_modules

Issue I'm Facing: Error found in components/main/Footer within the file path: ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue Components Tree as shown in the screenshot below: Cod ...

Is it feasible to determine the specific memory address of an Object in a program?

Is it possible in JavaScript to determine the memory location and memory usage of an object? Any assistance on this matter would be greatly appreciated. ...

Looking to add elements to a specific div dynamically using jQuery? Let's explore how to insert comments seamlessly

I would like to implement a comment system that adds entered comments to a specific div. Here's the code I have so far: <ul class="comments"> <li> <a class="commenter_name" href="/">Dushyanth Lion</a> ...

Subscribing to real-time updates using Next JS and Supabase sets the state to "closed"

Currently, I am building a helpdesk system for a school project using Next JS and Supabase. However, I have encountered an issue with the real-time chat functionality between the operator and the client. My approach involves subscribing to a table in the ...

What is the process for sending tinyEditory's content using ajax?

I recently incorporated the StfalconTinymceBundle into a Symfony2 project and it is functioning perfectly. However, I am encountering a problem when trying to submit the editor's content through AJAX. The editor's content does not seem to be bind ...

Navigate to the top of the page using Vue Router when moving to a new

Currently, in my Vue application, whenever I click on a <router-link>, it directs me to the new page but at the same scroll position as the previous one. This happens because the link only replaces the component. I am curious to know if there is a s ...

Navigating through a series of items in VueJS can be easily accomplished by using a

Below is the Vue instance I have created: new Vue({ el: '#app', data: { showPerson: true, persons: [ {id: 1, name: 'Alice'}, {id: 2, name: 'Barbara'}, {id: 3, name: &a ...

Error in Uploading Files with AngularJS - Uncaught SyntaxError: Unexpected token

I'm having trouble uploading an image file using Angular and PHP, specifically at the app.service Error line: app.service('fileUpload', ['$https:', function ($https:) { Code app.service('fileUpload', ['$https:&ap ...

Preventing the callback nightmare in nodeJs / Sharing variables with nested functions

Let's simplify this scenario: const generateUrl = (req, res) => { const id = req.query.someParameter; const query = MyMongooseModel.findOne({'id': id}); query.exec((err, mongooseModel) => { if(err) { / ...

Is it possible to assign multiple ID's to a variable in jQuery?

I am currently using a script for a slider known as Slicebox, and in order to have different image sizes for mobile and desktop views, I need to duplicate the feature on my website. Although it's not ideal, I really like this slider and want to explo ...