Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 611 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
React - changing an uncontrolled input

#1
I have a simple react component with the form which I believe to have one controlled input:

import React from 'react';

export default class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {}
}

render() {
return (
<form className="add-support-staff-form">
<input name="name" type="text" value={this.state.name} onChange={this.onFieldChange('name').bind(this)}/>
</form>
)
}

onFieldChange(fieldName) {
return function (event) {
this.setState({[fieldName]: event.target.value});
}
}
}

export default MyForm;

When I run my application I get the following warning:

> Warning: MyForm is changing an uncontrolled input of type text to be
> controlled. Input elements should not switch from uncontrolled to
> controlled (or vice versa). Decide between using a controlled or
> uncontrolled input element for the lifetime of the component

I believe my input is controlled since it has a value. I am wondering what am I doing wrong?

I am using React **15.1.0**
Reply

#2
> I believe my input is controlled since it has a value.

For an input to be controlled, its value must correspond to that of a state variable.

That condition is not initially met in your example because `this.state.name` is not initially set. Therefore, the input is initially uncontrolled. Once the `onChange` handler is triggered for the first time, `this.state.name` gets set. At that point, the above condition is satisfied and the input is considered to be controlled. This transition from uncontrolled to controlled produces the error seen above.

By initializing `this.state.name` in the constructor:

e.g.

this.state = { name: '' };

the input will be controlled from the start, fixing the issue. See [React Controlled Components][1] for more examples.

Unrelated to this error, you should only have one default export. Your code above has two.

[1]:

[To see links please register here]

"Controlled Components"
Reply

#3
Another approach it could be setting the default value inside your input, like this:

<input name="name" type="text" value={this.state.name || ''} onChange={this.onFieldChange('name').bind(this)}/>
Reply

#4
If the props on your component was passed as a state, put a default value for your input tags

<input type="text" placeholder={object.property} value={object.property ? object.property : ""}>
Reply

#5
Set a value to 'name' property in initial state.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

this.state={ name:''};

<!-- end snippet -->

Reply

#6
One potential downside with setting the field value to "" (empty string) in the constructor is if the field is an optional field and is left unedited. Unless you do some massaging before posting your form, the field will be persisted to your data storage as an empty string instead of NULL.

This alternative will avoid empty strings:

constructor(props) {
super(props);
this.state = {
name: null
}
}

...

<input name="name" type="text" value={this.state.name || ''}/>
Reply

#7
When you use **`onChange={this.onFieldChange('name').bind(this)}`** in your input you must declare your state empty string as a value of property field.

**incorrect way:**

this.state ={
fields: {},
errors: {},
disabled : false
}

**correct way:**

this.state ={
fields: {
name:'',
email: '',
message: ''
},
errors: {},
disabled : false
}
Reply

#8
In my case, I was missing something really trivial.

`<input value={state.myObject.inputValue} />`

My state was the following when I was getting the warning:

```
state = {
myObject: undefined
}
```

By alternating my state to **reference the input of my value**, my issue was solved:


```
state = {
myObject: {
inputValue: ''
}
}
```
Reply

#9
This generally happens only when you are not controlling the value of the filed when the application started and after some event or some function fired or the state changed, you are now trying to control the value in input field.

This transition of not having control over the input and then having control over it is what causes the issue to happen in the first place.

The best way to avoid this is by declaring some value for the input in the constructor of the component.
So that the input element has value from the start of the application.
Reply

#10
Simple solution to resolve this problem is to set an empty value by default :


<input name='myInput' value={this.state.myInput || ''} onChange={this.handleChange} />
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through