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

标签 javascript reactjs

以下是我学习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/

相关文章:

javascript - 在 Coffeescript 类的函数中指定第 n 个参数为 "super"

javascript - 使用 Next.js 无需重新加载页面即可更改路线

javascript - 如何从action中调用reducer中存储的变量?

reactjs - 未定义的架构符号, react 原生 cocoapod 问题

javascript - 返回未定义的 ajax 函数

javascript - Blogger IFRAME 从 URL 接收 URL_argument

javascript - 与语义 UI 侧边栏作斗争

javascript - 如何将 <tr> 插入到 <table> 中(使用 PHP)

javascript - 当使用通过 props 传递的函数时 setState 不起作用

javascript - 如何根据 API 数据设置初始 React 状态?