javascript - 为什么我在尝试运行代码时收到类型错误?

标签 javascript reactjs debugging

我试图弄清楚为什么我收到无法解构'this.props.customer'的属性'id',因为它未定义。错误。从外观上看,我的代码似乎是正确的,但尽管如此,我仍然收到上述错误。有什么我忽略的小事吗?

这是 CustomerList.js 文件:

import React, { Component } from "react";
import Customer from "./Customer";

class CustomerList extends Component {
    render() {
        const customers = this.props.customers;
        return(
            <div className="data">
                <table className="ui celled table">
                    <thead>
                        <tr>
                            <th style={{ width: '50px', textAlign: 'center' }}>#</th>
                            <th>Name</th>
                            <th>E-mail</th>
                            <th style={{ width: '148px' }}>Action</th>
                        </tr>
                    </thead>

                    <tbody>
                    {
                        customers.map(customer => {
                            return <Customer customer={customer} key={customer.id} />;
                        })
                    }
                        <Customer/>
                    </tbody>
                </table>
            </div>
        );
    }
}

export default CustomerList;

这是Customer.js:

import React, { Component } from 'react';

class Customer extends Component {
    render() {
        const { id, first_name, last_name, email } = this.props.customer;
        return (
            <tr>
                <td style={{ textAlign: 'center' }}>{id}</td>
                <td>{`${first_name} ${last_name}`}</td>
                <td>{email}</td>
                <td>
                    <button className="mini ui blue button">Edit</button>
                    <button className="mini ui red button">Delete</button>
                </td>
            </tr>
        );
    }
}

export default Customer;

最佳答案

map 部分下面有一个

<Customer/>

Customer 组件的调用没有参数,因此 customer 未定义。这就是您收到错误的原因。

关于javascript - 为什么我在尝试运行代码时收到类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60895575/

相关文章:

调试程序集以查找引用游戏中值的静态指针

c# - 我可以防止无效的小型转储文件名吗

javascript - 可折叠按钮 - Bootstrap 3.3.4

javascript - 使用 useState react hook 后返回的状态变量显示 .map 不是函数

javascript - 从可能不存在的 JSON 对象中提取数据

html - 为什么我的 HTML 标记会删除它前面的空格,为什么这里的换行符不起作用?

java - 为什么我不能在 SAXParser 中打开这个 XML?

javascript - 在 JavaScript 中删除文件

javascript - Apollo /graphQL : How to use optimistic UI for a mutation of a nested react component?

javascript - react : unnecessary component update