未捕获的类型错误和已修复的数据表

Uncaught TypeError and Fixed Data Table

本文关键字:数据表 错误 类型      更新时间:2023-09-26

我尝试用这段代码运行固定数据表。

   var MyCompi=React.createClass({
   getInitialState: function() {
   return{ rows : [{"id":1,"first_name":"William","last_name":"Elliott","email":"welliott0@wisc.edu",
"country":"Argentina","ip_address":"247.180.226.89"},
{"id":2,"first_name":"Carl","last_name":"Ross","email":"cross1@mlb.com",
"country":"South Africa","ip_address":"27.146.70.36"},
{"id":3,"first_name":"Jeremy","last_name":"Scott","email":"jscott2@cbsnews.com",
"country":"Colombia","ip_address":"103.52.74.225"}] };
   },

  render:function(){
    return(
 <Table
      height={this.state.rows.length * 300}
      width={1150}
      rowsCount={this.state.rows.length}
      rowHeight={30}
      headerHeight={30}
      rowGetter={function(rowIndex) {return this.state.rows[rowIndex]; }}>
      <Column dataKey="id" width={50} label="Id" />
      <Column dataKey="first_name" width={200} label="First Name" />
      <Column  dataKey="last_name" width={200} label="Last Name" />

    </Table>
      );
  }
});
ReactDOM.render(<MyCompi/>
   ,
    document.getElementById('root')
);

但我犯了错误。

  • Uncaught TypeError:无法读取未定义的属性"rows"

当我像"rows[]"那样设置getInitialState(rows)时,异常就会消失。但在这种情况下,我得到了空的数据表。

我真的对此感到困惑。有什么帮助吗?

rowGetter函数中,this不引用组件。您可以使用箭头功能:

rowGetter={rowIndex => this.state.rows[rowIndex]}

了解有关this和此问题的更多信息:如何在回调中访问正确的"this"上下文?