最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】ReactJS出错:Warning: Failed form propType You provided a value prop to a form field without an onChange handler

ReactJS crifan 2009浏览 0评论

ReactJS项目,在跳转到主页面后,出错:

warning.js:35 Warning: Failed form propType: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`. Check the render method of `Main`.

虽然不影响逻辑,但是还是要去解决。

去项目中搜:

form

 找到一个:

                        <input
                          type="text"
                          className="form-control pull-right"
                          value={this.formatYearMonth(this.state.curYearMonth)}
                          onClick={this.handleClickMonthInput}
                        />

好像不算是form

但是结果还真的是,注释掉这段代码,就不会出错了:

把:

className="form-control pull-right"

改为:

className="pull-right"

问题依旧。

Warning: Failed form propType You provided a value prop to a form field without an onChange handler

javascript – Failed form propType: You provided a `value` prop to a form field without an `onChange` handler – Stack Overflow

Uncontrolled Components – React

javascript – ReactJS error warning – Stack Overflow

加上onChange试试:

                        <input
                          type="text"
                          className="form-control pull-right"
                          value={this.formatYearMonth(this.state.curYearMonth)}
                          onClick={this.handleClickMonthInput}
                          onChange={this.onYearMonthInputChange}
                        />

结果就可以了。

React知识点滴

http://blog.leanote.com/post/sams/React知识点滴

“input必须有onChange事件绑定”

ctheu.com | ReactJS: A new way to build a website

【总结】

此处的写法:

                        <input
                          type="text"
                          className="form-control pull-right"
                          value={this.formatYearMonth(this.state.curYearMonth)}
                          onClick={this.handleClickMonthInput}
                        />

之所以报错:

Warning: Failed form propType: You provided a `value` prop to a form field without an `onChange` handler.

是因为:

React中,有两种Component:

受控组件 Controlled Components

必须参数:

  • input,select,textbox:value

  • checkbox,radio:checked

非受控组件 Uncontrolled Components

必须参数:

  • input,select,textbox, :defaultValue

  • checkbox,radio:defaultChecked

所以此处是input,且打算控制其输入到值,所以去加上onChange即可:

                        <input
                          type="text"
                          className="form-control pull-right"
                          value={this.formatYearMonth(this.state.curYearMonth)}
                          onClick={this.handleClickMonthInput}
                          onChange={this.onYearMonthInputChange}
                        />

转载请注明:在路上 » 【已解决】ReactJS出错:Warning: Failed form propType You provided a value prop to a form field without an onChange handler

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
56 queries in 0.095 seconds, using 18.31MB memory