简体   繁体   English

为什么我不能更改 React 中的输入字段值?

[英]Why can't I change the input field value in React?

The current problem I am having is that after retrieving and setting the value, I am not able to edit the value in the input field after retrieving it.我目前遇到的问题是,在检索和设置值后,我无法在检索后编辑输入字段中的值。 The value seems to be static and uneditable in the input field.该值似乎是 static 并且在输入字段中不可编辑。

I want have to have flexibility to change the input field value even after retrieving the value.即使在检索值之后,我也希望能够灵活地更改输入字段值。

Below is just a snippet of my code as to post the entire code is very large.下面只是我的代码片段,因为发布整个代码非常大。 I believe the problem lies inside the render() where i set the value={something} and handleInputChange .我相信问题出在我设置value={something}handleInputChangerender()内部。

Can someone tell me what is wrong with my code?有人可以告诉我我的代码有什么问题吗?

constructor(props) {
    super(props);

    this.state = {
        seedURL: '',
        response: null,
        error: null, 
        loading: false
    };
    this.handleInputChange = this.handleInputChange.bind(this);
}

handleInputChange = (e) => {
    e.preventDefault();

    let name = e.target.name;
    let value = e.target.value;

    this.setState({
        [name]: value
    });
};



render() {
    const name = this.state.response ? this.state.response.data.name : "";
    const image = this.state.response ? this.state.response.data.image : "";

    return(
        <form>
            <input type="text" placeholder="Business Name" name="name" onChange={this.handleInputChange} value={name} />
            <input type="text" placeholder="Image URL" name="image" onChange={this.handleInputChange} value={image} />
        </form>
    );
}

name and image are not defined in the state of your component.名称和图像未在组件的 state 中定义。

 this.state = {
        seedURL: '',
        response: null,
        error: null, 
        loading: false
    };

You could add them, eg您可以添加它们,例如

 this.state = {
        seedURL: '',
        response: null,
        error: null, 
        loading: false,
        image: '',
        name: ''
    };

But you will have to bind to this properties in the inputs like this:但是您必须在输入中绑定到此属性,如下所示:

<input type="text" placeholder="Business Name" name="name" onChange={this.handleInputChange} value={this.state.name} />

Alternatively, if you want to change the value of your response property of the state then you could use the spread operator to set the state of this object.或者,如果要更改 state 的响应属性的值,则可以使用扩展运算符设置此 object 的 state。

this.setState({
  ...this.state,
  response: {
    ...this.state.response,
    data: {
      ...this.state.response.data,
      [name]: e.target.value
    }
  }
})

That is because you're setting the value of input but that value is not changing when you type, to change those values you'll have to modify your setState() call to this:那是因为您正在设置inputvalue ,但在您键入时该值没有改变,要更改这些值,您必须修改您的setState()调用:

this.setState({
  response: {
    ...this.state.response,
    data: {
      ...this.state.response.data,
      [name]: [value]
    }
  }
})

Hope this helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM