Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Input Type not editable

Tags:

reactjs

I was adding dynamic values to the input file in react then I tried to edit that but it not at all editable.

var shop_profile_data = this.state.data.DETAILS;  <input id="shopname" className="inputMaterial"  value={shop_profile_data.NAME} type="text" required/> 

Please give me the solution. Thanks

like image 259
Anandhakumar R Avatar asked Mar 17 '16 17:03

Anandhakumar R


People also ask

Why is the input field not editable in React?

You need to add an onChange event and then set the shop_profile_data.NAME to a different value. Then the value of the input will change. If you only want to set the initial value of the input , use defaultValue property (docs). defaultValue will set the initial value, but then allow the value to be changed.

How do you set a readonly in React?

By default, the Rating is shown and it's readonly value is set to false . To enable the Rating readonly mode set it's readonly property to true .


2 Answers

Since value is always rendered with the same value (shop_profile_data.NAME) nothing is able to change. By setting value property you are making input a Controlled Component.

You need to add an onChange event and then set the shop_profile_data.NAME to a different value. Then the value of the input will change.

If you only want to set the initial value of the input, use defaultValue property (docs). defaultValue will set the initial value, but then allow the value to be changed.

For more read about Controlled vs Uncontrolled Components.

like image 153
Davin Tryon Avatar answered Oct 01 '22 18:10

Davin Tryon


Try using defaultValue instead of value, defaultValue will set the value and also editable. The code will look like this:

<input id="shopname" className="inputMaterial"  defaultValue={shop_profile_data.NAME} type="text" required/> 

reference: Uncontrolled Components – React

like image 28
Roy Ryando Avatar answered Oct 01 '22 19:10

Roy Ryando