hey guys, I'm wondering what is the accepted way to obtain the values of the inputs within a form in React + TS, I got to this answer but I wonder if there are more elegant ways to do it:
import { FormEvent } from 'react'
import './App.css'
function App() {
const onSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const usernameInput = (e.target as HTMLFormElement).elements.namedItem('username') as HTMLInputElement;
const passwordInput = (e.target as HTMLFormElement).elements.namedItem('password') as HTMLInputElement;
const password = passwordInput.value;
const username = usernameInput.value;
console.log('Username:', username);
console.log('Password:', password);
};
return (
<form onSubmit={onSubmit}>
<label htmlFor="username">Username</label>
<input name="username" type='text'/>
<label htmlFor="password">Password</label>
<input name="password" type='password'/>
<button type='submit'>Next</button>
</form>
)
}
export default App
Thanks!