javascript - React JS - 如何使用组件显示数据集?

标签 javascript reactjs

在 ContactList 组件中,我无法初始化为 Contact 元素数组并显示数据集:

  import React from 'react';      
  var ContactForm = React.createClass({
            render: function(){
              return (
                <tr>
                  <td>
                  <input type="text" className="form-control" />
                  </td>
                  <td>
                  <input type="text" className="form-control"/>
                  </td>
                  <td>
                  <input type="text" className="form-control" />
                  </td>
                  <td>
                  <input type="button" className="btn btn-primary" value="Add"/>
                  </td>
                </tr>
                )
            }
          });    
        var Contact = React.createClass({
              render: function(){
                  return (
                          <tr >
                             {this.props.contact.name}
                             {this.props.contact.address}
                             {this.props.contact.phone_number}
                          </tr>

                    ) ;
                }
              });

        var ContactList = React.createClass({
              render: function(){
                  var contactRows = this.props.list.map( item => {
               return <Contact key={item.name} contact={item} /> //something wrog here
           }); 
                        return (
                      <tbody >
                          {contactRows}
                          <ContactForm />
                      </tbody>
                    ) ;
                }
              });

        var ContactsTable = React.createClass({
              render: function(){
                  return (
                    <table className="table table-bordered">
                          <thead>
                            <tr>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Phone Number</th>
                            </tr>
                          </thead>
                          <ContactList contacts={this.props.contacts}  />
                    </table>
                    );
              }
          });
       var ContactsApp = React.createClass({
          render: function(){
              return (
                    <div>
                       <h1>Contact List.</h1>
                       <ContactsTable list ={this.props.contacts}  /> 
                    </div>
              );
          }
      });
//=====================================================//
index.js 

import '../node_modules/bootstrap/dist/css/bootstrap.css';  
import React from 'react';
import ReactDOM from 'react-dom';
import ContactsApp from './App';

     var contacts = [
            {
                "name": "Contact 1",
                "address": "123 Test St",
                "phone_number": "132-3212"
            },

            {
                "name": "Contact 2",
                "address": "23 Main St",
                "phone_number": "934-4329"
            },

            {
                "name": "Contact 3",
                "address": "4 Lower St",
                "phone_number": "432-5832"
            },

            {
                "name": "Contact 4",
                "address": "49 Upper Street",
                "phone_number": "934-4290"
            }
          ] ;       

    ReactDOM.render(
      <ContactsApp  contacts={contacts}  />,
      document.getElementById('root')
    );

最佳答案

<ContactsTable list ={this.props.contacts}  /> 

在上面的行中,您传递了 list,但在类中您尝试访问 this.props.contacts。将两者更改为 listcontacts:

<ContactList contacts={this.props.contacts}  />
             ^^^ your prop name is list

也在这里:

<ContactList contacts={this.props.contacts}  />

您已经传递了 contacts,但随后尝试在 map 调用的类中访问 this.props.list:

var contactRows = this.props.list.map( item => {
                             ^^^^ your prop name is contacts

关于javascript - React JS - 如何使用组件显示数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42817292/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'offsetTop' of null

javascript - 如何在react-router v4中使用当前路由获取渲染子菜单?

javascript - 将状态从子组件传递到父组件

javascript - 如何在屏幕的特定位置创建一个 div?

javascript - 如何将 CSS 应用于 document.write()?

javascript - 移动滚动时延迟元素位置更新

javascript - 在react-router中创建动态链接列表

reactjs - Redux - 如何调用一个 Action 并等待它被解决

reactjs - 如何使用 react-create-app 获取代码覆盖率报告?

javascript - 暂停执行代码,直到异步函数返回所需的值