gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'name' of undefined - Reactjs

转载 作者:行者123 更新时间:2023-12-03 01:35:21 25 4
gpt4 key购买 nike

以下是我学习react时写的代码,还没到使用state的地步。在练习props的使用时,遇到如下错误:Error snapshot

我无法弄清楚为什么该 Prop 显示为未定义。我正处于理解 React 的非常基础的阶段,希望能得到任何帮助。谢谢

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import moment from 'moment';
import PropTypes from 'prop-types';

let tweet = {
message: "Something about cats.",
gravatar: "xyz",
author: {
handle: "catperson",
name: "IAMA Cat Person"
},
likes: 2,
retweets: 5,
timestamp: "2016-07-30 21:24:37"
};

let from = {
name: "john doe",
addressLine1: "fake house, fake lane",
addressLine2: "fake city, fake state"
}

let to = {
name: "jane doe",
addressLine1: "fake-ish house, fake-ish lane",
addressLine2: "fake-ish city, fake-ish state"
}

function Tweet( {tweet} ) {
return (
<div className="tweet">
<Avatar hash={tweet.gravatar}/>
<div className="content">
<NameWithHandle author={tweet.author}/><Time time={tweet.timestamp}/>
<Message text={tweet.Message}/>
<div className="buttons">
<ReplyButton/>
<RetweetButton count={tweet.retweets}/>
<LikeButton count={tweet.likes}/>
<MoreOptionsButton/>
</div>
</div>
</div>
);
}

function Avatar( {hash} ) {
var url = `https://www.gravatar.com/avatar/${hash}`;
return (
<img src={url} className="avatar" alt="avatar" />
);
}

function Message( {text} ) {
return (
<div className="message">
{text}
</div>
);
}

function NameWithHandle( {author} ) {
const { name, handle } = author
return (
<span className="name-with-handle">
<span className="name">{name} </span>
<span className="handle">@{handle}</span>
</span>
);
}

// The {} is used when there is assignment going on inside the expression the component is pointing to,
// The () is used when there is nothing except a return of JSX from the component.
// If there is use of {} then the return statement is mandatory to return JSX and render.

const Time = ( {time} ) => {
const timeString = moment(time).fromNow();
return <span className="time">{timeString}</span>;
}

const ReplyButton = () => <i className="fa fa-reply reply-button"/>

const RetweetButton = ( {count} ) => (
<span className="retweet-button-span">
<span className="retweet-button-icon"><i className="fa fa-retweet retweet-button"/></span>
{getRetweetCount(count)}
</span>
);

const LikeButton = ( {count} ) => (
<span className="like-button-span">
<span className="like-button-icon"><i className="fa fa-heart like-button"/></span>
{count > 0 &&
<span className="like-count">
{count}
</span>}
</span>
);

const MoreOptionsButton = () => <i className="fa fa-ellipsis-h more-options-button"/>

function getRetweetCount(count) {
if(count > 0) {
return (
<span className="retweet-count">
{count}
</span>
);
} else {
return null;
}
}

const AddressLabel = ( {person} ) => {
const {name, addressLine1, addressLine2} = person;
return(
<div className = "addressLabel">
<div className = "person-name"> {name} </div>
<div className = "person-address1"> {addressLine1} </div>
<div className = "person-address2"> {addressLine2} </div>
</div>
);
}

const Envelope = ( {from}, {to} ) => (
<div>
<AddressLabel person={from}> From: </AddressLabel>
<AddressLabel person={to}> To: </AddressLabel>
</div>
);



LikeButton.propTypes = {
count: PropTypes.number
};

RetweetButton.propTypes = {
count: PropTypes.number
};

Time.propTypes = {
time: PropTypes.string
};

Message.propTypes = {
text: PropTypes.string
};

NameWithHandle.propTypes = {
author: PropTypes.shape({
name: PropTypes.string.isRequired,
handle: PropTypes.string.isRequired
}).isRequired
};

Avatar.propTypes = {
hash: PropTypes.string.isRequired
};

ReactDOM.render(
<div>
<Tweet tweet = {tweet}/>
<Envelope from = {from} to = {to}/>
</div>,
document.querySelector('#root')
);

最佳答案

您必须从 Envelope 中的同一个 props 对象解构 fromto:

const Envelope = ( {from, to} ) => (
<div>
<AddressLabel person={from}> From: </AddressLabel>
<AddressLabel person={to}> To: </AddressLabel>
</div>
);

关于javascript - 未捕获的类型错误 : Cannot read property 'name' of undefined - Reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51098073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com