【问题标题】:Uncaught TypeError: Cannot read property 'props' of null in React Class [duplicate]未捕获的类型错误:无法在 React 类中读取 null 的属性“props”[重复]
【发布时间】:2016-11-04 15:32:25
【问题描述】:

我已经重构了一个组件,我不再在类方法中使用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

【问题讨论】:

    标签: javascript reactjs ecmascript-6 es6-class


    【解决方案1】:

    你需要像这样在构造函数中将方法绑定到组件实例

    export default class Comments extends React.Component {
      constructor() {
        super();
    
        this.renderComment = this.renderComment.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    

    如果你在 stage-2 中使用 babel,你也可以重构你的方法,只需执行以下操作:

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

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

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

    【讨论】:

    • 在这种情况下,您甚至不需要 e 周围的括号 :-)
    • 您可以更新我粘贴的组件以接受答案吗?我试图将方法绑定到构造函数中的组件,但我仍然收到错误“无法读取未定义 @finalfreq 的属性 'bind'
    • 我的错,答案已接受
    【解决方案2】:

    你应该有一个构造函数,调用super()并在那里绑定方法

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

    【讨论】:

    • “ES6 自动将this 绑定到类,而不是反应组件。” 这是什么意思? ES6 类自动绑定。而 ES6 的类 React 组件。
    • 对不起,我写了一个毫无意义的东西。我的意思是,在 React.createClass 中,例如在 change 事件回调中,this 将自动绑定到组件,但在 ES6 类中,您必须手动进行,并且在 @987654328 之前不能使用 this @ 已被调用。是对的吗?如果不是,请纠正我
    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2022-01-19
    相关资源
    最近更新 更多