Javascript React应用程序错误:`Uncaught TypeError:无法读取属性';参考文献';空的`
我将发布组件的代码:Javascript React应用程序错误:`Uncaught TypeError:无法读取属性';参考文献';空的`,javascript,reactjs,webpack,Javascript,Reactjs,Webpack,我将发布组件的代码: 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.re
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>
)
}
}
您应该为
createPost
设置this
,因为在React中的ES2015
类没有,但当您使用React.createClass
class AddPostForm extends React.Component {
constructor(props) {
super(props);
this.createPost = this.createPost.bind(this);
}
....
}
我没有注意到这只发生在ES2015上。谢谢
class AddPostForm extends React.Component {
constructor(props) {
super(props);
this.createPost = this.createPost.bind(this);
}
....
}