I have a React functional component with a form, onSubmit I want to loop through all form elements....
export default function TransactionFilter() {
return (
<form onSubmit={handleSubmit}>
<TextField id="username" /><br/>
<TextField id="password" /><br/>
</form>
)
const handleSubmit = (event) => {
event.preventDefault();
var formElements = event.target.elements
formElements.forEach(element =>
console.log(`I found this ${element}`)
);
But this code is giving me an error...
react-dom.development.js:476 Uncaught TypeError: formElements.forEach is not a function
at handleSubmit (FilterForm.js:49)
at HTMLUnknownElement.callCallback (react-dom.development.js:337)
Am I not using forEach in the right way? If not, then what is the correct way to iterate over form elements array in React ?