简体   繁体   English

ReactJS - 类型错误:obj 未定义

[英]ReactJS - TypeError: obj is undefined

When rendering, I get a TypeError that my object is not defined.渲染时,我收到一个 TypeError,指出我的对象未定义。 But I thought I defined it before using it.但我想我在使用它之前定义了它。

This what my ArticleDetails.js that I am calling looks like.这就是我调用的 ArticleDetails.js 的样子。


import React, {Component} from 'react';


class ArticleDetail extends Component {
    render(){
        const obj = this.props.articleDetail;
        return(
            <div style={{ color: "yellow", border: "1px solid yellow" }}>
                <h4>{obj.title}</h4>
                <h4>{obj.body}</h4>
            </div>
        );
    }
}

export default ArticleDetail;


This is the article list that links to article details page.
`class ArticleList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            articlesData: [],
            article: "",
            showComponent: false,
        };
        this.getArticleDetail = this.getArticleDetail.bind(this);
        this.showArticleDetails = this.showArticleDetails.bind(this);
    }


    getArticleDetail(item) {
        axios
            .get('http://localhost:8000/'.concat(item.absolute_url))
            .then((response) => {
                this.setState({ title: response.data});
            })
            .catch(function (error) {
                console.log(error);
            })
    }


    showArticleDetails(item) {
        this.getArticleDetail(item);
        this.setState({ showComponent: true });
    }


    componentDidMount(){
        axios
            .get('http://localhost:8000/')
            .then((response) => {
                this.setState({ articlesData: response.data });
            })
            .catch(function (error) {
                console.log(error);
            })      
    }


    render(){
        return(
            <div>
                {this.state.articlesData.map( item => {
                    return (
                        <h3 key={item.id} onClick={() => this.showArticleDetails(item)}>
                            {item.title}, {item.short_description}
                        </h3>
                    );
                })}

                {this.state.showComponent ? (
                    <ArticleDetail articleDetails={this.state.article} />
                    ) : null}
            </div>
        );
    }
}

export default ArticleList;`

I am a newbie and have not been able to proceed past this.我是新手,无法继续进行。 I appreciate all help that will help me make sure that I properly defined "obj."感谢所有帮助我确保正确定义“obj”的帮助。 Please ELI5.请ELI5。 I am very new to ReactJS.我对 ReactJS 很陌生。

const obj = this.props.articleDetail and <ArticleDetail articleDetails={this.state.article} /> const obj = this.props.articleDetail<ArticleDetail articleDetails={this.state.article} />

articleDetail and articleDetails articleDetailarticleDetails

Do you see the difference in the names?你看到名字的区别了吗? It seems you just missed the s at the end.看来你只是错过了最后的 s 。 Try to add.尝试添加。

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

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