简体   繁体   English

反应数据/道具未定义

[英]React data/props undefined

So i am working on a pretty simple landing page Based on reactjs. 所以我正在基于Reactjs的一个非常简单的登陆页面上工作。 So there is a section where i have our speakers component. 所以有一部分我有我们的扬声器组件。 When i try to map through the speakers it shows that data is undefined 当我尝试通过扬声器映射时,表明数据未定义

import Speakers from "./components/Speakers";

class App extends Component {
  state = {
    speakers: {
      socials: ["facebook", "twitter", "dribbble"],
      data: [
        {
          img:
            "https://bootstrapmade.com/demo/themes/Groovin/assets/img/dummies/team1.jpg",
          name: "Jeiii",
          details:
            "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Corporismolestiae debitis, distinctio, ratione dignissimos ea quia liberofacilis, accusantium voluptates eveniet mollitia vitae nostrum animi.Cupiditate nam alias praesentium perspiciatis sint rerum, culpa vero veritatis! Magnam doloremque iste esse? Voluptas.",
          social: "dribble"
        },
        {
          img:
            "https://bootstrapmade.com/demo/themes/Groovin/assets/img/dummies/team1.jpg",
          name: "Jeiii",
          details:
            "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Corporismolestiae debitis, distinctio, ratione dignissimos ea quia liberofacilis, accusantium voluptates eveniet mollitia vitae nostrum animi.Cupiditate nam alias praesentium perspiciatis sint rerum, culpa vero veritatis! Magnam doloremque iste esse? Voluptas.",
          social: "dribble"
        },
        {
          img:
            "https://bootstrapmade.com/demo/themes/Groovin/assets/img/dummies/team1.jpg",
          name: "Jeiii",
          details:
            "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Corporismolestiae debitis, distinctio, ratione dignissimos ea quia liberofacilis, accusantium voluptates eveniet mollitia vitae nostrum animi.Cupiditate nam alias praesentium perspiciatis sint rerum, culpa vero veritatis! Magnam doloremque iste esse? Voluptas.",
          social: "dribble"
        }
      ]
    },

  };
  render() {
    const { hero, event, schedule, speakers } = this.state;
    return (
      <Fragment>
        <Container>
            <Speakers
              img={speakers.img}
              name={speakers.name}
              details={speakers.details}
              socials={speakers.socials}
            />
          </div>
        </Container>
      </Fragment>
    );
  }
}

And here is the speaker component where the error is showing data is undefined 这是扬声器组件,其中错误显示数据是未定义的

class Speakers extends Component {
  render() {
    const { data, socials } = this.props;
    const speakerList = data.map(speaker => {
      return (
        <Col md={3}>
          <Card>
            <CardImg src={speaker.img} className="img-fluid" top width="100%" />
            <CardBody>
              <CardTitle className="name text-center">{speaker.name}</CardTitle>
              <CardText className="details">{speaker.details}</CardText>
              <div>
                {socials.map(social =>
                  speaker.socials[social] ? (
                    <a href={`https:www.${speaker.socials}.com`}>
                      <i className={`fab fa-${social}`} />
                    </a>
                  ) : null
                )}
              </div>
            </CardBody>
          </Card>
        </Col>
      );
    });

    return (
      <div className="speakers">
        <Row>{speakerList}</Row>
      </div>
    );
  }
}

enter image description here 在此处输入图片说明

Let's see what is wrong with this. 让我们看看这有什么问题。

1.You are not sending data prop to Speakers Component. 1.您没有将数据道具发送到扬声器组件。

<Speakers
   img={speakers.img}
   name={speakers.name}
   details={speakers.details}
   socials={speakers.socials}
 />

Do like this. 这样吧

<Speakers
   img={speakers.img}
   name={speakers.name}
   details={speakers.details}
   socials={speakers.socials}
   data={speakers.data}
 />

2.speakers state does not have properties like name,img,details as whole.They are part of data object inside speakers state.So speakers.name ,speakers.details won't work. 2.speakers状态没有像name,img,details一样的属性。它们是speaker状态内数据对象的一部分。所以speakers.name,speakers.details不起作用。

What you need to know. 您需要知道的。

a.Destructuring just happens at top level.It won't give you details inside the data object inside. a。分解只是在顶层进行,不会为您提供内部数据对象的详细信息。

b.Always use prop-types for better prop types checking. b。始终使用道具类型来更好地检查道具类型。

You are sending wrong data from your App component, it should be 您从App组件发送了错误的数据,应该是

 render() {
    const { socials, data } = this.state;
    return (
      <Fragment>
         <Speakers socials={socials} data={data}/>
      </Fragment>
    );
  }

or 要么

 render() {
    return (
      <Fragment>
         <Speakers ...this.state/>
      </Fragment>
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM