简体   繁体   English

React.JS Typescript - OnChange 表示状态对象的“组件正在将类型文本的受控输入更改为在 OnChange 中不受控制”

[英]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.

相关问题 使用非状态变量时的React.js由于组件正在更改要控制的文本类型的不受控制的输入而导致错误 - React.js when using non state variable Getting error as A component is changing an uncontrolled input of type text to be controlled 警告:组件正在将文本类型的受控输入更改为不受控制。 (React.js) - Warning: A component is changing a controlled input of type text to be uncontrolled. (React.js) React - 一个组件正在将文本类型的受控输入更改为不受控制 - React - A component is changing a controlled input of type text to be uncontrolled 组件将文本类型的受控输入更改为不受控制 - A component is changing a controlled input of type text to be uncontrolled 组件正在更改要控制的文本类型的不受控输入? - A component is changing an uncontrolled input of type text to be controlled? 警告:组件正在将受控输入更改为反应 js 中的不受控输入 - Warning: A component is changing a controlled input to be uncontrolled input in react js 电子邮件输入警告-组件将文本类型的受控输入更改为不受控制 - Email Input Warning - A component is changing a controlled input of type text to be uncontrolled 组件正在将文本类型的受控输入更改为不受控制。 输入元素不应从受控切换到不受控 - A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled 反应组件正在更改要控制的复选框类型的不受控制的输入 - React a component is changing an uncontrolled input of type checkbox to be controlled 警告:组件正在更改要控制的文本类型的不受控制的输入 - Warning: A component is changing an uncontrolled input of type text to be controlled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM