简体   繁体   English

react.js 中的道具问题

[英]Issue with props in react.js

I am new to react.我是新来的反应。 I am working on building a simple contact list application to display the contact's name, email and we will store a catchphrase of a contact.我正在构建一个简单的联系人列表应用程序来显示联系人的姓名 email,我们将存储联系人的标语。 We will make use of this , it contains a JSON dump of data needed for our contact list application.我们将使用,它包含我们的联系人列表应用程序所需的数据的 JSON 转储。 I have 2 files statushelper.js and shapes.js我有 2 个文件 statushelper.js 和 shape.js

Below is the statushelper.js下面是 statushelper.js

import React, {Component} from 'react';
import Shapes from './Shapes';

class StatusHelper extends Component {
    constructor() {
        super();

        this.state = {
            contacts: []
        };
    }
    componentDidMount() {
        fetch('http://jsonplaceholder.typicode.com/users')
            .then(res => res.json())
            .then((data) => {
                this.setState({contacts: data})
            })
            .catch(console.log)
    }
    render () {
        return (
            <Shapes contacts={this.state.contacts} />
        );
    }
}

export default StatusHelper;

Shapes.js形状.js

import React from 'react'

const Shapes = ({ contacts }) => {
    return (
        <div style={{text_align: 'center'}}>
            <h1>Contact List</h1>
            {contacts.map((contact) => (
                <div className="card">
                    <div className="card-body">
                        <h5 className="card-title">{contact.name}</h5>
                        <h6 className="card-subtitle mb-2 text-muted">{contact.email}</h6>
                        <p className="card-text">{contact.company.catchPhrase}</p>
                    </div>
                </div>
            ))}
        </div>
    )
};

export default Shapes

When I run statushelper.js file I am running into the following errors当我运行 statushelper.js 文件时,我遇到了以下错误

'contacts' is missing in props validation                react/prop-types
 Missing "key" prop for element in iterator               react/jsx-key

any suggestions?有什么建议么?

You are rendering an array of elements, so React needs a key prop.你正在渲染一个元素数组,所以 React 需要一个 key prop。

Add index to your mapping and key={index} to your div "card".index添加到您的映射并将key={index}添加到您的 div“卡片”。 Like that:像那样:

const Shapes = ({ contacts }) => {
    return (
        <div style={{text_align: 'center'}}>
            <h1>Contact List</h1>
            {contacts.map((contact, index) => (
                <div className="card" key={index}>
                    <div className="card-body">
                        <h5 className="card-title">{contact.name}</h5>
                        <h6 className="card-subtitle mb-2 text-muted">{contact.email} 
                        </h6>
                        <p className="card-text">{contact.company.catchPhrase}</p>
                    </div>
                </div>
            ))}
        </div>
    )
};

These are not errors but simply warnings.这些不是错误,而只是警告。 To fix these warnings.修复这些警告。 Do the following:请执行下列操作:

1) You are missing prop types that's why you have the first error. 1)您缺少道具类型,这就是您遇到第一个错误的原因。

Do this in shapes.js :shapes.js中执行此操作:

import PropTypes from 'prop-types';

// Component declaration

Shapes.propTypes = {
    contacts: React.PropTypes.arrayOf(React.PropTypes.shape({
      name: React.PropTypes.string,
      email: React.PropTypes.string,
      company: React.PropTypes.shape({
          catchPhrase: React.PropTypes.string
      })
   }))
};

2) You're missing key in your map function which react uses for optimisation : Just change 2)您的map function 中缺少用于优化的键:只需更改

{contacts.map((contact) => (
                <div className="card">

to

{contacts.map((contact, index) => (
                <div className="card" key={index}>

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

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