在子组件中反应调用父组件方法,这在parrent方法中是未定义的(打字稿)

时间:2018-04-16 17:30:23

标签: reactjs typescript

我有两个组成部分。

我在父组件中发送showModalDelete方法作为提示给我的子元素。

import * as React from 'react';
import Table from './../components/Table';
import Delete from './../components/Delete';

class User extends React.Component<any, any> {
  static urlUsers : string = 'http://localhost:52968/employee/api/index';
  static urlDelete : string = 'http://localhost:52968/employee/api/delete/';
  static tableHeader = ['Id','First name', 'Surname','Email','Phone number',''];
  static columns = ['id','firstName', 'lastName','email','phoneNumber'];
  private delete: any;

    constructor( props: any) {
      super(props); 
      this.state = { users: []};
      console.log(this);
      this.showModalDelete.bind(this);
    }

    componentWillMount() {

    }

    showModalDelete() {
      console.log(this);
      this.delete.handleShow();
    }

    render() {   
      console.log(this);
      return (
        <div>
          <Table 
            show={this.showModalDelete} 
            url={User.urlUsers} 
            header={User.tableHeader}
            columns={User.columns}
          />
          <Delete ref={(del) => { this.delete = del; }} />
        </div>

      );
    }
  }
export default User;

然后在我的子组件中定义onClick方法来调用parrent方法。 (this.props.show)

import * as React from 'react';
import * as $ from 'jquery';

class Table extends React.Component<any,any> {

  constructor(props: any) {
      super(props);
      this.state = { items: []};
  }

  componentWillMount() {
      this.loadItems();
  }

  loadItems() {
    var self: Table;
    self = this;
    $.ajax (
      {
        url: self.props.url,
        dataType: 'json',
        cache:false,
        success:
         function(items: any) {
          self.setState({items:items});
        },
        error: function(xhr: any, status: any, err: any) {
            console.log(xhr);
        }
      });
    }    

  render() {
    return (
    <table className="table row-border stripe no-footer">
        <thead>
          <tr>
              {
                this.props.header.map((item:string,index: number) =>
                <th key={index}>{item}</th>
                )
              }
          </tr>
        </thead>

        <tbody>
        {
            this.state.items.map((item:any) =>
                <tr key={item.id}>
                    {
                        this.props.columns.map((column:string,index: number) => 
                            <td key={index}>{item[column]}</td>
                        )                   
                    }   
                    <td>
                        <a 
                            onClick={this.props.show} 
                            className="glyphicon glyphicon-trash table-buttons table-buttons-delete" 
                        />
                    </td>             
                </tr>
            )
        }
        </tbody>       
      </table>
    );
  }
}

export default Table;

当调用show modal delete时,这是未定义的。 我究竟做错了什么?我很困惑如何在打字稿中使用它。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

.bind方法将返回一个绑定this新函数。它不会改变你调用它的函数。所以你的构造函数绑定不会重新分配方法,所以它就好像你从来没有调用过绑定一样。

要解决此问题,您只需在调用bind时重新分配方法:

this.showModalDelete = this.showModalDelete.bind(this);