[英]React.JS Typescript - OnChange says "A component is changing a controlled input of type text to be uncontrolled in OnChange" for State Object
Good day,再会,
I'm new with react.js
I'm trying to create a basic data binding using onChange
of the input.我是
react.js
我正在尝试使用输入的onChange
创建一个基本的数据绑定。 The problem is, I'm assigning to object with it's properties.问题是,我将其属性分配给对象。 Not directly to the property.
不直接到物业。
Now I'm receiving the error Warning: A component is changing a controlled input of type text to be uncontrolled.
现在我收到错误
Warning: A component is changing a controlled input of type text to be uncontrolled.
when I type-in a character in my inputs.当我在输入中输入一个字符时。
Here's my code:这是我的代码:
interface IProps { }
interface IFloorInfo {
id: number
name: string,
type: string,
condition: string
}
interface IFloorInfoState {
floor: IFloorInfo
}
export default class Floors extends React.Component<IProps, IFloorInfoState> {
state: IFloorInfoState
constructor(props: any){
super(props)
this.state = {
floor: {
id: 0,
name: '',
type: '',
condition: ''
}
}
}
...
render() {
return (
<div>
<input type="text" value={this.state.floor.name} onChange={(e)=>this.inputChanges(e)} />
<input type="text" value={this.state.floor.type} onChange={(e)=>this.inputChanges(e)} />
<input type="text" value={this.state.floor.condition} onChange={(e)=>this.inputChanges(e)} />
</div>
)
}
}
Now this is my inputChanges
method that detects if there's a changes in the input
现在这是我的
inputChanges
方法,用于检测input
是否发生变化
inputChanges = (e:any) => {
this.setState({ floor: e.target.value });
}
Thank you in advance.先感谢您。
The problem is with your following code.问题在于您的以下代码。 According to this code, your state will be
{floor: "input value"}
根据此代码,您的状态将是
{floor: "input value"}
inputChanges = (e:any) => {
this.setState({ floor: e.target.value });
}
But what you actually want is但你真正想要的是
inputChanges = (e:any) => {
// copying all values of floor from current state;
var currentFloorState = {...this.state.floor};
// setting the current input value from user
var name = e.target.name;
currentFloorState[name] = e.target.value;
this.setState({ floor: currentFloorState });
}
As for multiple properties: You can add name property to your element and use it in your changeHandler
至于多个属性:您可以将 name 属性添加到您的元素并在您的
changeHandler
使用它
render() {
return (
<div>
<input type="text" value={this.state.floor.name} name="floor" onChange={(e)=>this.inputChanges(e)} />
<input type="text" value={this.state.floor.type} name="type" onChange={(e)=>this.inputChanges(e)} />
</div>
)
}
For demo, you can refer this https://codesandbox.io/s/jolly-ritchie-e1z52对于演示,你可以参考这个https://codesandbox.io/s/jolly-ritchie-e1z52
In this code, you don't specify which property that you want to bind.在此代码中,您没有指定要绑定的属性。
inputChanges = (e:any) => {
this.setState({ floor: e.target.value });
}
What you can do, is something like this.你能做的,就是这样。
inputChanges = (e:any) => {
this.setState({
...this.state,
floor: { ... this.state.floor, [e.currentTarget.name]: e.currentTarget.value}
})
}
Basically, you're binding whatever property that matches inside your this.state.floor
object.基本上,您正在绑定与
this.state.floor
对象内匹配的任何属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.