【问题标题】:which is the correct way to acces the props? [closed]访问道具的正确方法是什么? [关闭]
【发布时间】:2022-01-25 03:52:57
【问题描述】:

我认为我没有正确访问属性 src,这可以在像这样的一行中的所有对象中找到

徽标:src={imgrep(1)} alt="altofem"

每当我尝试让它们访问 src 时,我都会收到错误 × typeerror cannot read property of undefined (reading 'props')

自从这个错误来来去去,我已经卡了一个星期:(,提前谢谢你!

//IMPORTS
import React, { useState } from "react";
import { imgrep } from "../../Helper/imgrep";
import styled from "styled-components";
import "react-circular-progressbar/dist/styles.css";
import Cards from "./Cards";
import { FaRegEye, VscGraphLine, BsFileEarmarkText } from 'react-icons/all';
import { memerep } from "../../Helper/memes";
import RocketLaunchOutlinedIcon from '@mui/icons-material/RocketLaunchOutlined';
import { CircularProgressbar, buildStyles } from 'react-circular-progressbar';
import 'react-circular-progressbar/dist/styles.css';


const ShowAndHide = () => {
//ARRAY
  const professions = [
     {   
    circular: <CircularProgressbar value= "99" text={`99%`} 
    styles={buildStyles({
     // Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
     strokeLinecap: 'butt',
     // Text size
     textSize: '16px',
     // How long animation takes to go from one nother, in seconds
     pathTransitionDuration: 1.5,
     // Colors
     pathColor: `rgba(58, 123, 213, ${99 / 100})`,
     textColor: '#102647',
     trailColor: '#e0e0eb',
     backgroundColor: '#3e98c7',
   })}
   /> ,
      key: "card11",
      project:"PROYECTO4 Mat",
      icon: "",
      specialty: "CV",
      industry: "Reciclaje",
      summary: "Estimación de huella de carbono (gramo CO2 eq) con 
                detección de materiales de reciclaje.",
      logo: <img  className="Mediumround" src={imgrep(11)} alt="ecosale" />,
    },


    { 
    circular: <CircularProgressbar value= "99" text={`99%`} 
    styles={buildStyles({
     // Whether to use rounded or flat corners on the ends - can use 
 'butt' or 'round'
     strokeLinecap: 'butt',
     // Text size
     textSize: '16px',
     // How long animation takes to go from one nother, in seconds
     pathTransitionDuration: 1.5,
     // Colors
     pathColor: `rgba(58, 123, 213, ${99 / 100})`,
     textColor: '#102647',
     trailColor: '#e0e0eb',
     backgroundColor: '#3e98c7',
   })}
   /> ,
      key: "card12",
      project:"PROYECTO",
      icon: "",
      specialty: "unn",
      industry: "it",
      summary: "ti",
      logo: <img  className="Mediumsquare" src={imgrep(12)} alt="unitti" />,
    },
    {  
    circular: <CircularProgressbar value= "99" text={`99%`} 
    styles={buildStyles({
     // Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
     strokeLinecap: 'butt',
     // Text size
     textSize: '16px',
     // How long animation takes to go from one nother, in seconds
     pathTransitionDuration: 1.5,
     // Colors
     pathColor: `rgba(58, 123, 213, ${99 / 100})`,
     textColor: '#102647',
     trailColor: '#e0e0eb',
     backgroundColor: '#3e98c7',
   })}
   /> ,
      key: "card13",
      project:"PROYECTO",
      icon: "",
      specialty: "u",
      industry: "a",
      summary: "i",
      logo: <img  id="uai" className="Bigsquare" src={imgrep(13)} alt="uai" />,
    },
  ];

  const [myProfession, setMyProfession] = useState("");

   return (
    <>
      {/* INFORMATION CARDS */}
      <Container>
        <LeftSide>
          <Bottom>
            {professions &&(
          <Cards 
          circular={myProfession.circular}
          project={myProfession.project}
          icon={myProfession.icon}
          percentage={myProfession.percentage} 
          specialty= {myProfession.specialty}
          industry={myProfession.industry}
          summary={myProfession.summary}
          rocket={myProfession.rocket}
          **id={myProfession.logo.props.id}
          src={myProfession.logo.props.src}
          className={myProfession.logo.props.className}**
           />       
            )}
            
            {professions.map((profession) => ( 
              <info
              circular={profession.circular}
              project={profession.project}
              icon={profession.icon}
              percentage={profession.percentage}
              specialty={profession.specialty}
              industry={profession.industry}
              summary={profession.summary}
              **id={profession.logo.props.id}
              src={profession.logo.props.src}
              className={profession.logo.props.className}**
              onMouseEnter={() => setMyProfession(profession.logo.props.alt)}/>
            ))}
          </Bottom>
        </LeftSide>
        {/* HOVERING LOGOS */}
        <RightSide>
          <h2> - Nuestros Casos de Exito -</h2>
          <br />
          <Buttons>
            {professions.map((profession) => (
              <>

/// here it renders just fine!///
                <img
                  type="img"
                  key={profession}
                  id={profession.logo.props.id}
                  src={profession.logo.props.src}
                  className={profession.logo.props.className}
                  onMouseEnter={() => setMyProfession(profession)}
                ></img>
              </>
            ))}
          </Buttons>
        </RightSide>
      </Container>
    </>
  );
};

export default ShowAndHide;

【问题讨论】:

  • 不清楚你在试图做什么:id={myProfession.logo.props.id}你设置为myProfession的初始值是false这里:const [myProfession, setMyProfession] = useState(false);和@ 987654326@ 没有名为.logo 的属性。所以myProfession.logo当然是undefined

标签: javascript arrays reactjs react-props


【解决方案1】:

类组件

import React, {Component} from 'react';

class Home1 extends Component {

 constructor(props){
   super(props);
   console.log(props);
 }

 render(){
  return(<div>Hello, {this.props.name}</div>);
 }
}

export default Home1;

功能组件

import React from 'react';

const Home2 = (props) => {
 
 return(<div>Hello, {props.name}</div>);
}

export default Home2; 

休息一下,在你冗长的代码中,你可能会发现哪里出了问题——我在这两种类型的组件(类和函数式)中为你提供了道具的基本可访问性(或如何访问它们)。

【讨论】:

  • 你应该正确使用useState钩子,它是职业对象而不是boolean变量。并检查它是否为undefined 并将其传递给Cards 组件。
猜你喜欢
  • 2019-03-30
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多