reactjs - 如何在函数中读取 this.props

标签 reactjs null ecmascript-6

我是 ES6 的 ReactJS 新手,问题是我无法读取 onDelete () 函数内的 'this.props.productId' 属性,这是错误: DeleteProductComponent.js:15 Uncaught TypeError: Cannot read property 'props' of null 谁能给我一个最佳解决方案或告诉我我做错了什么?谢谢

 import React, { Component } from 'react';

    class DeleteProductComponent extends Component {

     componentDidMount() {

        $('.page-header h1').text('Delete Product');

        }

onDelete(){

var id = this.props.productId;

        $.ajax({
            url: "http://localhost/my-app/public/delete.php",
            type : "POST",
            contentType : 'application/json',
            data : JSON.stringify({'id' : id}),
            success : function(response) {
                this.props.changeAppMode('read');
            }.bind(this),
            error: function(xhr, resp, text){
                // show error in console
                console.log(xhr, resp, text);
            }
        });
    }

    // handle save changes button here
    // handle save changes button clicked

      render () {

        return (
            <div className='row'>
                <div className='col-md-3'></div>
                <div className='col-md-6'>
                    <div className='panel panel-default'>
                        <div className='panel-body text-align-center'>Are you sure?</div>
                        <div className='panel-footer clearfix'>
                            <div className='text-align-center'>
                                <button onClick={this.onDelete}
                                    className='btn btn-danger m-r-1em'>Yes</button>
                                <button onClick={() => this.props.changeAppMode('read')}
                                    className='btn btn-primary'>No</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div className='col-md-3'></div>
            </div>
        );
      }

    } 
    export default DeleteProductComponent; 

最佳答案

通过两种方式将 onDelete() 函数绑定(bind)到组件:

构造函数中:

class DeleteProductComponent extends Component {

  constructor(props) {
    super(props);
    this.onDelete = this.onDelete.bind(this);
  }

  componentDidMount() {}

  onDelete() {}

  render() {}

}

或者使用ES6的语法来实现onDelete()(然后它会自己绑定(bind)到组件):

 onDelete = () => {

    var id = this.props.productId;

    $.ajax({
        url: "http://localhost/my-app/public/delete.php",
        type : "POST",
        contentType : 'application/json',
        data : JSON.stringify({'id' : id}),
        success : function(response) {
            this.props.changeAppMode('read');
        }.bind(this),
        error: function(xhr, resp, text){
            // show error in console
            console.log(xhr, resp, text);
        }
    });
  }

关于reactjs - 如何在函数中读取 this.props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43956861/

相关文章:

javascript - 如何为使用 withRouter 包裹的组件定义 propTypes?

javascript - 为什么拖放 Js 会破坏 php 文件的上传过程?

java - 无法使用 findViewById 检索 View

javascript - 在 Javascript 中按日期和时间对对象数组进行排序时出现问题 - 允许空值

javascript - Node.js 生成参数导致 git update-index 失败

javascript - 使用 LODASH 比较 2 个数组来查找缺失的信息

linux - 我的服务器没有监听文件更改

reactjs - 从前端将图像上传到 Azure blob (React)

reactjs - 如何让我的 React Web 应用程序出现在谷歌搜索中?

java - netbeans 和 java,资源 Map.getString 返回 null