Forms in JS
submitting forms in JS the most common and important task to do in javascript
- use JS dom manipulation and js fetch() method
const username = document.getElementById('username').value
const response = await fetch('https://myexampleapi.xyz/v1/todo/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
add error handling and such.
React
nextjs typescript
- custom hook package like react-hook-form ⭐️ . other options like formik
- hold input values in react state
- most of them support schema validation with libs like
yuporzod
the way we did it in genXC is the 2nd option (holding values in react state)
const [form, setForm] = useState({ email: '', password: '' });
const inputChange = (e: any) => setForm({ ...form, [e.target.id]: e.target.value });
//later on
<input id="email" type="email" required className="form-input" placeholder="Enter Email" onChange={inputChange} />