[英]Input fields values taken state cannot be modified
I cannot modify input predefined value.我无法修改输入预定义值。
constructor(props) {
super(props);
this.state = {
userDetails: this.props.location.state.userDetails,
token: "",
firstName: "",
lastName: "",
email: "",
roles: []
};
This is the method that I use for handling input:这是我用于处理输入的方法:
handleInputChange = (e) => {
const target = e.target;
const value = target.value;
const name = target.name;
this.setState = {
[name]: value,
};
}; };
This is the input element:这是输入元素:
<input
type="text"
value={this.state.userDetails.firstName}
name="firstName"
ref="firstName"
onChange={this.handleInputChange}
/>
In the UI I cannot delete its(input) default value taken from the state neither type something else.在 UI 中,我无法删除从 state 获取的(输入)默认值,也无法输入其他内容。 If I comment the value this problem disappears.
如果我评论这个值,这个问题就会消失。
What am I doing wrong in this case?在这种情况下我做错了什么?
<input>
element always reads its value from this.state.userDetails.firstName
, which doesn't change. <input>
元素总是从this.state.userDetails.firstName
读取它的值,它不会改变。
Because of that, <input>
's value has to be firstName
:因此,
<input>
的值必须是firstName
:
<input
type="text"
value={this.state.firstName}
...
Then you can, for instance, set firstName: this.props.location.state.userDetails.firstName
as default value on first render.然后,例如,您可以将
firstName: this.props.location.state.userDetails.firstName
设置为首次渲染时的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.