Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created March 19, 2024 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/0326c8c8e3e28f4905caf561b20407fb to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/0326c8c8e3e28f4905caf561b20407fb to your computer and use it in GitHub Desktop.
Sample Form starter for React
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