0

我正在尝试构建一个简单的 Rails 后端并响应前端博客应用程序。一切都很顺利,直到我尝试编辑帖子时才开始编辑操作。我无法设置 propTypes 并在表单中使用 refs。我收到以下错误

[![see the below image link to see the error details][1]][1]


[1]: https://i.stack.imgur.com/VvdS0.png

我正在使用 react-rails gem

这是我的控制器编辑和更新操作

def edit
 @post = Post.find(params[:id])
end

def update
 if @post.update(post_params)
  render json: { post: @post }
else
 render :edit
end

结尾

这里edit.html.erb

<%= react_component 'EditPost', { post: @post.as_json(only: [:id, :title, :body]) } %>

下面是位于 assets/javascripts/components 中的 EditPost 组件

class EditPost extends React.Component {

  constructor(props) {
    super(props)

  }

  render () {

    return (
      <div className="row">
        <div className="col-md-6 col-md-offset-3">
          <h3>Post</h3>
          <form>
            <div className="form-group">
              <label htmlFor="title">Title</label>
              <input type="text"
                     className="form-control"
                     placeholder="title"
                     ref="title" /> // here I can't set ref
            </div> <br />
            <div className="form-group">
              <label htmlFor="body">Body</label>
              <input type="text"
                     className="form-control"
                     placeholder="body"
                     ref="body" /> // here I can't set ref
            </div> <br />
          </form>
        </div>
      </div>
    )
  }
}
// if I uncomment below code I will get (Uncaught TypeError: can not read property 'string' of undefined
// EditPost.propTypes = {
//   title: React.PropTypes.string.isRequired,
//   body: React.PropTypes.string.isRequired
// }

这是我的 github 存储库(https://github.com/Hano1388/react-rails-blog),请随意克隆它以查看我遇到的错误。谢谢,感谢您的帮助

4

1 回答 1

0

React 16 不再打包 propTypes。尝试使用:

import PropTypes from 'prop-types'

并改为PropTypes而不是React.PropTypes

title: PropTypes.string.isRequired,
于 2017-12-17T10:48:20.490 回答