Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS - Warning: A component is changing an uncontrolled input of type text to be controlled

I am trying to get rid off this error message, but still unsuccessfully.

Warning: A component 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.

There's also link to the Facebook page, but I am still not sure how to figure it out.

class EditItem extends Component {
    constructor(props) {
        super(props);
        this.state = {items: ''};

        this.addItemService = new ItemService();
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
  }

  componentDidMount(){
    axios.get('http://localhost:3005/items/edit/'+this.props.match.params.id)
    .then(response => {
      this.setState({ items: response.data });
    })
    .catch(function (error) {
      console.log(error);
    })
  }

  handleChange = (e) => {
    let items = Object.assign({}, this.state.items);    //creating copy of object
    items.item = e.target.value;                        //updating value
    this.setState({items});
  }

  handleSubmit(event) {
    event.preventDefault(); // not sure why this
    this.addItemService.updateData(this.state.items.item, this.props.match.params.id); // service for updating the data
    this.props.history.push('/index'); // redirect
  }

  render() {
    return (
          <div className="container">
            <form onSubmit={this.handleSubmit}>
              <label>
                Edit Item:
                <input type="text" value={this.state.items.item} className="form-control" onChange={this.handleChange}/>
              </label><br/>
              <input type="submit" value="Update" className="btn btn-primary"/>
            </form>
        </div>
    );
  }
}

In the input seems to be always a not-null value, how do I fix this?

like image 594
user2932090 Avatar asked Jul 18 '18 16:07

user2932090


People also ask

How do you fix a component is changing an uncontrolled input to be controlled?

Controlled inputs We can convert the above component to controlled by simply passing an empty string as the initial value to the useState hook.

What is an uncontrolled component in React Why would we use it instead of a controlled one?

In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself. To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.

What does a controlled component allow that an uncontrolled component does not?

Controlled component is component that get the changed value from the callback function and uncontrolled component is component that have the one from the DOM. For example, When input value is changed,we can use onChange function in Controlled Component and also we can get the value using DOM like ref.

How do you handle input change in React?

The standard way to handle form input value changes is to handle them with React. This is a technique called controlled components. We can create controlled components with input , textarea , and select elements. They maintain their own state, and updates are based on user input.


1 Answers

In the state items is defined as a string, and hence when you assign the value to text input like

<input type="text" value={this.state.items.item} className="form-control" onChange={this.handleChange}/>

You are essentially writing

<input type="text" value={undefined} className="form-control" onChange={this.handleChange}/>

for the initial render, and once the result of API call is available and items state changes to an object that contains item key, you are passing a value to input and hence converting it from uncontrolled to controlled, which is what the warning is about. In order to avoid the warning, you would simply initilise your state like

this.state = {items: { items: '' }};

or use input like

<input type="text" value={this.state.items.item || ''} className="form-control" onChange={this.handleChange}/>
like image 124
Shubham Khatri Avatar answered Sep 28 '22 01:09

Shubham Khatri