Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react use same useState for multiple inputs

const MyComponent = () => {
   const [inputs, setInputs] = useState({});
   const handleChange = e => setInputs(prevState => ({ ...prevState, [e.target.name]: e.target.value }));

   return (
     <> 
      <input name="field1" value={inputs.field1 || ''} onChange={handleChange} />
      <input name="field2" value={inputs.field2 || ''} onChange={handleChange} />
     </>
   )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: dynamically change css class with javascript 
Javascript :: return only specific attributes when making query mongoose 
Javascript :: how to do radio button validation in jquery 
Javascript :: To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. 
Javascript :: how to append the dropdown values by jquery each function 
Javascript :: javascript spread and rest operator 
Javascript :: python range in javascript 
Javascript :: how to take input in javascript in coding 
Javascript :: vs code open file in new window 
Javascript :: use ngfor to make a dropdown in angular from array 
Javascript :: javascript string contains 
Javascript :: regex pattern for strong password 
Javascript :: pxijs text 
Javascript :: javascript to remove duplicates from an array 
Javascript :: check if js property exists in class 
Javascript :: catch error message js 
Javascript :: pwa angular npm 
Javascript :: get time from date javascript 
Javascript :: next js material ui typescript 
Javascript :: get closest element id jquery 
Javascript :: java 8 add whole array to jsonarray 
Javascript :: js add class 
Javascript :: react aws s3 npm 
Javascript :: how remove last letter js 
Javascript :: How to Set Active Tab in jQuery Ui 
Javascript :: javascript js isNumber 
Javascript :: center element in react native 
Javascript :: js get clipboard data 
Javascript :: count number of each element in array javascript 
Javascript :: vscode react cannot find moudle when import image 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =