I have used TSX file for loading text box in React JS as below :
<input type={'text'} value={employees.length > 0 ? employees[0].name : ""} id=
{'Name'} label={'Name'} name={'Name'} htmlFor={'Name'} />
Now when I load this file then we cant write any thing in text box.
So anyone can please help me?
You cannot write anything because you have created a controlled component which means that the value of the input will always be whatever the
value
prop evaluates to - in this caseemployees[0].name
or""
.With some minor adjustments you can make this work, either by making it a controlled component with an event listener which updates the value, or by making it an uncontrolled component.
More info here and here.
Option 1 - Controlled component (recommended)
To make a controlled component, you need to add the
onChange
event listener and have that update the variable that you pass intovalue
. It's not clear from your question whatemployees
is; if it's a state or prop variable or something else.Assuming it's a state variable, you can do:
Option 2 - Uncontrolled component
You only need a slight modification to make your input an uncontrolled component. Simply replace
value
withdefaultValue
:As a side note, you don't need to wrap string literals in brackets. So instead of
<input type={'text'} ...
you can just do<input type='text' ...
, if you prefer.