未捕获的TypeError:无法在React类中读取null的属性'props'

时间:2016-11-04 15:32:25

标签: javascript reactjs ecmascript-6 es6-class

我已经重构了一个组件,我不再在类方法中使用React.createClass了,但我现在有这个错误

{this.props.removeComment.bind(null, this.props.params.svgId, i)}
  

TypeError:无法读取未定义的属性'props'

代码完美无缺

在重构之前

import React from 'react';

const Comments = React.createClass({
  renderComment(comment, i) {
    return (
      <div className="comment" key={i}>
        <p>
          <strong>{comment.user}</strong>
          {comment.text}
          <button className="remove-comment" onClick={this.props.removeComment.bind(null, this.props.params.svgId, i)}>&times;</button>
        </p>
      </div>
    )
  },

  handleSubmit(e) {
    e.preventDefault();
    const { svgId } = this.props.params;
    const author = this.refs.author.value;
    const comment = this.refs.comment.value;
    this.props.addComment(svgId, author, comment);
    this.refs.commentForm.reset();
  },

  render() {
    return (
      <div className="comments">
        {this.props.svgComments.map(this.renderComment)}
        <form ref="commentForm" className="comment-form" onSubmit={this.handleSubmit}>
          <input type="text" ref="author" placeholder="author"/>
          <input type="text" ref="comment" placeholder="comment"/>
          <input type="submit" hidden />
        </form>
      </div>
    )
  }
});

export default Comments;

现在在重构之后

import React from 'react';

export default class Comments extends React.Component {
  renderComment(comment, i) {
    return (
      <div className="comment" key={i}>
        <p>
          <strong>{comment.user}</strong>
          {comment.text}
          <button className="remove-comment" onClick={this.props.removeComment.bind(null, this.props.params.svgId, i)}>&times;</button>
        </p>
      </div>
    )
  };

  handleSubmit(e) {
    e.preventDefault();
    const { svgId } = this.props.params;
    const author = this.refs.author.value;
    const comment = this.refs.comment.value;
    this.props.addComment(svgId, author, comment);
    this.refs.commentForm.reset();
  };

  render() {
    return (
      <div className="comments">
        {this.props.svgComments.map(this.renderComment)}
        <form ref="commentForm" className="comment-form" onSubmit={this.handleSubmit}>
          <input type="text" ref="author" placeholder="author"/>
          <input type="text" ref="comment" placeholder="comment"/>
          <input type="submit" hidden />
        </form>
      </div>
    )
  }
};

那么如何在类构造函数中手动绑定this

2 个答案:

答案 0 :(得分:2)

您需要将方法绑定到构造函数中的组件实例,如此

export default class Comments extends React.Component {
  constructor() {
    super();

    this.renderComment = this.renderComment.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

如果您正在使用阶段2的babel,您还可以重构您的方法并执行以下操作:

renderComment = (comment, i) => {
  // code goes here    
}

handleSubmit = (e) => {
  // code goes here
}

我更喜欢第二种方式,因为它更干净,但必须有正确的插件让babel才能正常工作。

这样做的目的是确保在调用这些函数时,调用它们时this绑定到组件。

答案 1 :(得分:0)

你应该有一个构造函数,调用super()然后绑定方法

React.createClass会自动将其绑定到组件,在ES6类中你必须手动设置它,并且在调用this之前你不能使用super()