Created
March 19, 2024 13:55
-
-
Save prof3ssorSt3v3/0326c8c8e3e28f4905caf561b20407fb to your computer and use it in GitHub Desktop.
Sample Form starter for React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from 'react'; | |
export default function Form() { | |
const [firstName, setFirstName] = useState(''); | |
const [age, setAge] = useState(''); | |
const ageAsNumber = Number(age); | |
// ... | |
return ( | |
<form> | |
<div> | |
<label> | |
First name: | |
<input value={firstName} onChange={(e) => setFirstName(e.target.value)} /> | |
</label> | |
</div> | |
<div> | |
<label> | |
Age: | |
<input value={age} onChange={(e) => setAge(e.target.value)} type="number" /> | |
<button onClick={() => setAge(ageAsNumber + 10)}>Add 10 years</button> | |
</label> | |
</div> | |
<div> | |
{firstName !== '' && <p>Your name is {firstName}.</p>} | |
{ageAsNumber > 0 && <p>Your age is {ageAsNumber}.</p>} | |
</div> | |
</form> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment