React关于Value值警告的问题
index.js:1452 Warning: Failed prop type: You provided avalueprop to a form field without anonChangehandler. This will render a read-only field. If the field should be mutable usedefaultValue. Otherwise, set eitheronChangeorreadOnly.
in input (at App.js:95)
in div (at App.js:78)
in App (at src/index.js:7)
原因:
如果设置value,那value就永远是设置的那个值,要通过输入内容改变只能通过onChange,要么就把这个input设置成readOnly的
英文讲得很明白,如果你希望的是给字段赋初值那就用 defaultValue,如果用了类似
<input value={1} />这种,那这个输入框的值将永远为1,无法通过输入来更改,只能通过onChange来修改
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭