最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Undefined Error: TypeError: Cannot read properties of undefined (reading 'Icon') - Stack Overflow

programmeradmin0浏览0评论

I've been playing around with NextJS and TypeScript by watching a few videos and going through tutorials online. Until recently I have not run in to an issue, but am stuck on this. I've gone through my type.ts, resumeData.ts, and my Bar.tsx and my vsCode is not giving me any problem errors (of course I'm aware that it does not pick up everything).

I had no issue with the name, and level but for some reason seem to be missing why I am getting an undefined error in my Bar.tsx inside const Bar:... (besides the obvious of it saying it is not defined) and am unsure even where it should be defined or how.

I have gone ahead and posted each of the above mentioned files below.

Thanks for any help or suggestions on this.

Bar.tsx

import { FunctionComponent } from "react";
import { ISkill } from "../type";
import { motion } from "framer-motion";

const Bar: FunctionComponent<{ value: ISkill }> = ({
  value: { Icon, name, level },
}) => {
  const bar_width = `${level}%`;

  const variants = {
    initial: {
      width: 0,
    },
    animate: {
      width: bar_width,
      transition: {
        duration: 0.4,
        type: "spring",
        damping: 10,
        stiffness: 100,
      },
    },
  };

  return (
    <div className="my-2 text-white bg-gray-300 rounded-full dark:bg-dark-300 dark:bg-black-500">
      <motion.div
        className="flex items-center px-4 py-1 rounded-full bg-gradient-to-r from-green to-blue-600"
        style={{ width: bar_width }}
        variants={variants}
        initial="initial"
        animate="animate"
      >
        <Icon className="mr-3" />
        {name}
      </motion.div>
    </div>
  );
};

export default Bar;

type.ts

import { IconType } from "react-icons/lib";

export interface IService {
  Icon: IconType;
  title: string;
  about: string;
}

export interface ISkill {
  Icon: IconType;
  name: string;
  level: string;
}

export interface IProject {
  name: string;
  description: string;
  image_path: string;
  deployed_url: string;
  github_url: string;
  category: Category[];
  key_techs: string[];
}

export type Category = "react" | "mongo" | "express" | "django" | "node";

resumeData.ts

import { ISkill } from "../type";
import { BsCircleFill } from "react-icons/bs";

export const languages: ISkill[] = [
  {
    Icon: BsCircleFill,
    name: "Python",
    level: "70%",
  },
  {
    Icon: BsCircleFill,
    name: "JavaScript",
    level: "60%",
  },
  {
    Icon: BsCircleFill,
    name: "React Native",
    level: "80%",
  },
  {
    Icon: BsCircleFill,
    name: "React",
    level: "70%",
  },
  {
    Icon: BsCircleFill,
    name: "Django",
    level: "80%",
  },
  {
    Icon: BsCircleFill,
    name: "Bootstrap",
    level: "80%",
  },
];

export const tools: ISkill[] = [
  {
    Icon: BsCircleFill,
    name: "Figma",
    level: "85%",
  },
  {
    Icon: BsCircleFill,
    name: "Photoshop",
    level: "45%",
  },
  {
    Icon: BsCircleFill,
    name: "Illustrator",
    level: "60%",
  },
  {
    Icon: BsCircleFill,
    name: "Framer",
    level: "70%",
  },
];

I've been playing around with NextJS and TypeScript by watching a few videos and going through tutorials online. Until recently I have not run in to an issue, but am stuck on this. I've gone through my type.ts, resumeData.ts, and my Bar.tsx and my vsCode is not giving me any problem errors (of course I'm aware that it does not pick up everything).

I had no issue with the name, and level but for some reason seem to be missing why I am getting an undefined error in my Bar.tsx inside const Bar:... (besides the obvious of it saying it is not defined) and am unsure even where it should be defined or how.

I have gone ahead and posted each of the above mentioned files below.

Thanks for any help or suggestions on this.

Bar.tsx

import { FunctionComponent } from "react";
import { ISkill } from "../type";
import { motion } from "framer-motion";

const Bar: FunctionComponent<{ value: ISkill }> = ({
  value: { Icon, name, level },
}) => {
  const bar_width = `${level}%`;

  const variants = {
    initial: {
      width: 0,
    },
    animate: {
      width: bar_width,
      transition: {
        duration: 0.4,
        type: "spring",
        damping: 10,
        stiffness: 100,
      },
    },
  };

  return (
    <div className="my-2 text-white bg-gray-300 rounded-full dark:bg-dark-300 dark:bg-black-500">
      <motion.div
        className="flex items-center px-4 py-1 rounded-full bg-gradient-to-r from-green to-blue-600"
        style={{ width: bar_width }}
        variants={variants}
        initial="initial"
        animate="animate"
      >
        <Icon className="mr-3" />
        {name}
      </motion.div>
    </div>
  );
};

export default Bar;

type.ts

import { IconType } from "react-icons/lib";

export interface IService {
  Icon: IconType;
  title: string;
  about: string;
}

export interface ISkill {
  Icon: IconType;
  name: string;
  level: string;
}

export interface IProject {
  name: string;
  description: string;
  image_path: string;
  deployed_url: string;
  github_url: string;
  category: Category[];
  key_techs: string[];
}

export type Category = "react" | "mongo" | "express" | "django" | "node";

resumeData.ts

import { ISkill } from "../type";
import { BsCircleFill } from "react-icons/bs";

export const languages: ISkill[] = [
  {
    Icon: BsCircleFill,
    name: "Python",
    level: "70%",
  },
  {
    Icon: BsCircleFill,
    name: "JavaScript",
    level: "60%",
  },
  {
    Icon: BsCircleFill,
    name: "React Native",
    level: "80%",
  },
  {
    Icon: BsCircleFill,
    name: "React",
    level: "70%",
  },
  {
    Icon: BsCircleFill,
    name: "Django",
    level: "80%",
  },
  {
    Icon: BsCircleFill,
    name: "Bootstrap",
    level: "80%",
  },
];

export const tools: ISkill[] = [
  {
    Icon: BsCircleFill,
    name: "Figma",
    level: "85%",
  },
  {
    Icon: BsCircleFill,
    name: "Photoshop",
    level: "45%",
  },
  {
    Icon: BsCircleFill,
    name: "Illustrator",
    level: "60%",
  },
  {
    Icon: BsCircleFill,
    name: "Framer",
    level: "70%",
  },
];
Share Improve this question edited Feb 1, 2022 at 14:15 Ed Lucas 7,3654 gold badges40 silver badges50 bronze badges asked Feb 1, 2022 at 10:48 ransomenoteransomenote 5353 gold badges7 silver badges15 bronze badges 1
  • Can you show us the <Bar ... /> part of the parent's JSX? – user5734311 Commented Feb 1, 2022 at 10:52
Add a ment  | 

2 Answers 2

Reset to default 1
const Bar: FunctionComponent<{ value: ISkill }> = ({
  value: { Icon, name, level },
}) => {
// Your code ...
}

Here the error is ing from line 2: value: {Icon, name, level}. The value ing via props seems to be undefined. Check where the Bar ponent is used and if the props are passed properly.

Example:

<Bar value={{Icon: YourIcon, name: "...", level: "..."}}/>

If there's a possibility that Icon is undefined, you can always test for its value before attempting to include it in your JSX. This should at least eliminate the error.

{Icon && (<Icon className="mr-3" />)}

To determine why Icon is undefined in the first place, you have to look at the parent ponent that's including <Bar> to see where the Icon property is not properly being set in the value object.

发布评论

评论列表(0)

  1. 暂无评论