【问题标题】:Getting error "props.data.map is not a function" in React在 React 中出现错误“props.data.map 不是函数”
【发布时间】:2019-02-20 23:06:11
【问题描述】:

我正在尝试制作一个表单,提交时数据将显示在下表中。我已经创建了 Form 和 Table 组件并将表单数据传递给 Table 组件。在表格组件中,我创建了 2 个组件; TableHeader 和 TableBody 并将数据从 Table 传递到 TableBody。但是,我收到一个错误:

props.data.map is not a function

这是我的代码:

Form.js

import React, { Component } from 'react';
import DisplayTable from '../Components/Table';
import Jumbotron from 'react-bootstrap/Jumbotron';
import Container from 'react-bootstrap/Container';


class Form extends Component {
  constructor(){
    super();
    //1st 3 state fields will fetch the input data and next 3 will store the updated state into it
    this.state={

      first:'',
      last:'',
      email:'',
      firstU:'',
      lastU:'',
      emailU:''
    };
    this.onInputChange=this.onInputChange.bind(this);
    this.onInputSubmit=this.onInputSubmit.bind(this);
  }

  //on input change captures the values of the fields into respective input fields (first,last,email)
  onInputChange=(event)=>{
    this.setState({
      [event.target.name]:event.target.value
    }
    )
    //console.log(event.target.value);
   // console.log('input change running');
  }
//sets the values on submit
  onInputSubmit=()=>{
   // this.setState();

    //console.log(this.state.first);
   this.submitData(this.state.first,this.state.last,this.state.email);
    //console.log('input submit running');
  }
  //sets the  onChange state into new state fields
  submitData=(first,last,email)=>{
    this.setState({
      firstU:first,
      lastU:last,
      emailU:email
    });
    console.log('submitData running');
  }
  render() {
    return (

         <Jumbotron fluid>
        <Container>
            <h1>Form</h1>
            <div className="App" align="center">
        <div class="row">
          <input
            name="first"
            onChange={this.onInputChange}
            type="text"
            value={this.state.first}
          />
          </div>
        <div class="row">
          <input
            name="last"
            onChange={this.onInputChange}
            type="text"
            value={this.state.last}
          />
        </div>
        <div class="row">
          <input
            name="email"
            onChange={this.onInputChange}
            type="email"
            value={this.state.email}
          />
        </div>

        <div class="row">
          <button name="submit" onClick={this.onInputSubmit}>
            Submit
          </button>

        </div>

      </div>
      {/*here passed entire state into single variable instead of passing like 
      this.state.first,this.state.last to reduce code
       */}
          <DisplayTable data={this.state}/>
          </Container>
        </Jumbotron>

    );
  }
}

export default Form;

Table.js

import React, { Component } from 'react';
import Jumbotron from 'react-bootstrap/Jumbotron';
import Container from 'react-bootstrap/Container';
import Table from 'react-bootstrap/Table';

//props.data will fetch value from the state
class DisplayTable extends Component{
  render(){
    //console.log("hi"+this.props.data.first);
    //const data=this.props;
    return (

         <Jumbotron fluid>
        <Container>
            <h1>Data</h1>
           <TableHeader/>
           <TableBody data={this.props}/>

          </Container>
        </Jumbotron>

    );
  }
}

const TableHeader= ()=> {
  return(
      <div>
        <Table striped bordered hover variant="dark">
          <thead>
    <tr>
      <th>#</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>email</th>
    </tr>
  </thead>

      </Table>
      </div>
  );
}

const TableBody = (props)=> {
 console.log("k"+props.data.first)
 const rows=props.data.map((row,index)=>{
  return(

    <div>
       <Table striped bordered hover variant="dark">
      <tbody>
    <tr>
      <td key={index}></td>
      <td>{row.first}</td>
      <td>{row.last}</td>
      <td>{row.email}</td>
    </tr>
  </tbody>
  </Table>
    </div>
  );
 })
 return ({rows});
}

export default DisplayTable;

【问题讨论】:

  • 看来数据是你的状态对象。 .map() 用于遍历数组而不是对象。
  • @seanulus 哦,对了,数据不是数组。但是如何在没有任何数组的情况下打印 TableBody 中的值?我在打印 TableBody 的值时遇到错误

标签: javascript reactjs ecmascript-6


【解决方案1】:

解决此问题的一个好方法可能是像这样编辑您的状态:

this.state = {
  users: [
    {
      first: 'name',
      last: 'name',
      etc...
    },
    {
      first: 'name',
      last: 'name',
      etc...
    }   
  ],
  other: state //if need be
}

这样,您就有了一个用户数组,并且可以根据需要向表中添加更多用户。

然后你只需通过 props 将this.state.users 传递给你的子组件。

【讨论】:

    【解决方案2】:

    您的 props.data 是一个数组吗,因为 map 仅适用于数组。我认为您的数据在{} 中,但不在[] 中。请验证。

    打印数据进行验证。

    【讨论】:

    • 对了,数据不是数组。但是如何在没有任何数组的情况下打印 TableBody 中的值?我在打印 TableBody 的值时遇到错误
    • 您可以循环javascript对象或将对象转换为数组。如果您想使用地图,您可以将 {} 转换为 [ ]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 2020-09-20
    • 2023-04-02
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多