简体   繁体   English

React app error:`Uncaught TypeError:无法读取属性'refs'为null`

[英]React app error: `Uncaught TypeError: Cannot read property 'refs' of null`

I'll just post the code of the component: 我只是发布组件的代码:

class AddPostForm extends React.Component {
  createPost(event) {
    event.preventDefault();
    let post = {
      title : this.refs.title.value,
      name : this.refs.name.value,
      desc : this.refs.desc.value,
      image : this.refs.image.value
    }
    this.props.addPost(post);
    this.refs.postForm.reset();
  }
  render() {
    return (
      <div className="callout secondary form-area">
        <h5>Add a Post to the React Blog</h5>
        <form className="post-edit" ref="postForm" onSubmit={this.createPost}>
          <label>Post Title
            <input type="text" ref="title" placeholder="Post Title" required/>
          </label>
          <label>Author Name
            <input type="text" ref="name" placeholder="Full Name required" required/>
          </label>
          <label>Post Body
          <textarea
            ref="desc"
            placeholder="Write your post here" required/>
          </label>
          <label>Image URL - <span className="highlight">use this one to test 'http://bit.ly/1P9prpc'</span>
            <input type="url" ref="image" placeholder="The URL of the featured image for your post" required/>
          </label>
          <button type="submit" className="button">Add Post</button>
        </form>
      </div>
    )
  }
}

When the function createPost is triggered, the console logs an error: Uncaught TypeError: Cannot read property 'refs' of null . 当触发函数createPost ,控制台会记录一个错误: Uncaught TypeError:无法读取null的属性'refs'

Yet when I transform the code back to ES5, it works: 然而,当我将代码转换回ES5时,它可以工作:

var AddPostForm = React.createClass({
  createPost : function(event) {
    event.preventDefault();
    // take the data from the form and create an object
    var post = {
      title : this.refs.title.value,
      name : this.refs.name.value,
      desc : this.refs.desc.value,
      image : this.refs.image.value
    }
    // add the post to the App State
    this.props.addPost(post);
    this.refs.postForm.reset();
  },
  render : function() {
    return (
      <div className="callout secondary form-area">
      <h5>Add a Post to the React Blog</h5>
        <form className="post-edit" ref="postForm" onSubmit={this.createPost}>
          <label>Post Title
            <input type="text" ref="title" placeholder="Post Title" required/>
          </label>
          <label>Author Name
            <input type="text" ref="name" placeholder="Full Name required" required/>
          </label>
          <label>Post Body
          <textarea
            ref="desc"
            placeholder="Write your post here" required/>
          </label>
          <label>Image URL - <span className="highlight">use this one to test 'http://bit.ly/1P9prpc'</span>
            <input type="url" ref="image" placeholder="The URL of the featured image for your post" required/>
          </label>
          <button type="submit" class="button">Add Post</button>
        </form>
      </div>
    )
  }
});

You should set this for createPost , because with ES2015 classes in React there is no autobinding , but this feature exists when you use React.createClass 你应该为createPost设置this ,因为在React中有ES2015类没有自动autobinding ,但是当你使用React.createClass时这个特性就存在了

class AddPostForm extends React.Component {
   constructor(props) {
      super(props);
      this.createPost = this.createPost.bind(this);
   } 

   ....
}

Autobinding 自动绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React refs:无法读取 null 的属性“焦点” - React refs: Cannot read property 'focus' of null 反应-未捕获的TypeError:无法读取&gt; null的属性&#39;state&#39; - React - Uncaught TypeError: Cannot read property 'state' of > null 反应:未捕获的TypeError:无法读取null的属性“ sendTranslation” - React: Uncaught TypeError: Cannot read property 'sendTranslation' of null 反应未捕获的类型错误:无法读取 null 的属性“load” - REACT Uncaught TypeError: Cannot read property 'load' of null React tutorial:Uncaught TypeError:无法读取null的属性“data” - React tutorial: Uncaught TypeError: Cannot read property 'data' of null 反应选择:未捕获的类型错误:无法读取 null 的属性“offsetTop” - React-select: Uncaught TypeError: Cannot read property 'offsetTop' of null 未捕获的类型错误:无法读取 null React 的属性“setState” - Uncaught TypeError: Cannot read property 'setState' of null React 未捕获的TypeError:无法在react中读取null的属性'state' - Uncaught TypeError: Cannot read property 'state' of null in react 未被捕获的TypeError:无法在React中读取null的属性&#39;value&#39; - Uncaught TypeError: Cannot read property 'value' of null in React React:Uncaught TypeError:无法读取null的属性&#39;_currentElement&#39; - React: Uncaught TypeError: Cannot read property '_currentElement' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM