简体   繁体   English

无法在React中读取未定义错误的属性“名称”

[英]cannot read property 'name' of undefined error in React

I am creating a simple contact React app and I have created a link of for the name which will the go to a Show component. 我正在创建一个简单的联系React应用程序,并且已经创建了名称链接,该链接将转到Show组件。

However, when I click on the link it is saying {contactId: "undefined"} and {contact: undefined} 但是,当我单击链接时,它说的是{contactId: "undefined"}{contact: undefined}

Below is my code for the Contact component. 下面是我的Contact组件代码。

import React from 'react';

import { Link } from 'react-router';

const Contact = props =>
<div className= 'pure-u-1-3'>
    <h1>
        <Link to={`/contacts/${props.id}`}>
        {props.name}
        </Link>
    </h1>
    <h2>{props.email}</h2>

 </div>;

 export default Contact;

Not sure if the error is in this component or if it is in the Collections Component where it collects all the contacts? 不确定错误是在此组件中还是在收集所有联系人的集合组件中?

Here is the data component which holds the data. 这是保存数据的数据组件。

const data = [
{ id: 1, name: 'brian patterson', email: 'brian@hotmail.com',},
{ id: 2, name: 'katy patterson', email: 'katy@hotmail.com',},
{ id: 3, name: 'petra verkaik', email: 'petra@hotmail.com',}
];

export default data;

The collections component renders all the data so I am not sure if the error is in this part this code. 集合组件将呈现所有数据,因此我不确定该代码是否在此部分中。

import React from 'react';
import Contact from './Contact';
import data from './data';
import './Collection.css';




 class Layout extends React.Component {
 componentWillMount() {
    this.setState({
        contacts: data,
    });
}

addContact = (e) => {
    e.preventDefault();
    const contacts = this.state.contacts;
    const newId = contacts[contacts.length - 1].id + 1
    this.setState({
        contacts: contacts.concat({ id: newId, name: this.refs.name.value, email: this.refs.email.value }),
    });
    console.log('clicked!!');
    this.refs.name.value = null;
    this.refs.email.value = null;
}
newContact = () =>
<div className= 'pure-g'>
<div className='pure-u-12-24'> 
  <form className='pure-form' onSubmit={this.addContact}>
      <fieldset>

          <legend>New Contact</legend>

          <input ref='email' type="email" placeholder='example@example.com' />
          <input ref='name' type="text" placeholder='Name' />
          <button type="submit" class="pure-button pure-button-   primary">Add</button>
      </fieldset>
  </form>
  </div>
 </div>;
   render() {
    return (
    <div id='Collection'>
     {this.newContact()}
        <div className='pure-g'>
          {this.state.contacts.map(info =>
            <Contact key={info.id} name={info.name} email={info.email} />
            )}
        </div>
    </div>
    );
  }
}

export default Layout;

Once the link has been click it goes to a Show component and this the code in the Show.js component. 单击链接后,它将转到一个Show组件,这是Show.js组件中的代码。

import React from 'react';


import data from './data';

 class Show extends React.Component {
  componentWillMount() {
    console.log(this.props.params);
    this.setState({
     contact: data.filter(c => c.id === parseInt(this.props.params.contactId,    10))[0],
    });
   }
   render() {
    console.log(this.state)
    return (
    <div id='Show'>
      <h1>{this.state.contact.name}</h1>
    </div>

    );
   }
  }

export default Show;

It doesn't look like you are passing the id to the Contact component. 似乎您没有将id传递给Contact组件。 It should be something like: 应该是这样的:

<Contact key={info.id} id={info.id} name={info.name} email={info.email} /> then you can use props.id in the component. <Contact key={info.id} id={info.id} name={info.name} email={info.email} />则可以在组件中使用props.id

Also where is contactId defined or used? 另外, contactId在哪里定义或使用?

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

相关问题 类型错误:无法读取未定义反应错误的属性“名称” - TypeError: Cannot read property 'name' of undefined react error React Typescript:第 0 行:解析错误:无法读取未定义的属性“名称” - React Typescript: Line 0: Parsing error: Cannot read property 'name' of undefined 反应错误类型错误:无法读取未定义的属性“名称” - React Error TypeError: Cannot read property 'name' of undefined 无法读取 React state 中未定义的属性“名称” - Cannot read property 'name' of undefined in React state 类型错误:无法读取未定义的属性“名称”-反应 - TypeError: Cannot read property 'name' of undefined - react 反应:类型错误:无法读取未定义的属性“名称” - REACT: TypeError: Cannot read property 'name' of undefined TypeError:无法读取未定义反应的属性“名称” - TypeError: Cannot read property 'name' of undefined react 无法读取未定义的属性“名称”-React Native - Cannot read property 'name' of undefined - React Native 无法读取未定义的属性“名称” - 反应 - Cannot read property 'name' of undefined - react React Native:错误:“无法读取未定义的属性‘名称’,”虽然包含名称的对象不是未定义的 - React Native: Error:"Cannot read property 'name' of undefined, " Although object which contains name is not undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM