scrimba
Frontend Career Path
React basics
Meme generator
Forms in React: Radio buttons
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Forms in React: Radio buttons
AboutCommentsNotes
Forms in React: Radio buttons
Expand for more info
Form.js
run
preview
console
import React from "react"

export default function Form() {
const [formData, setFormData] = React.useState(
{
firstName: "",
lastName: "",
email: "",
comments: "",
isFriendly: true
}
)

function handleChange(event) {
const {name, value, type, checked} = event.target
setFormData(prevFormData => {
return {
...prevFormData,
[name]: type === "checkbox" ? checked : value
}
})
}

return (
<form>
<input
type="text"
placeholder="First Name"
onChange={handleChange}
name="firstName"
value={formData.firstName}
/>
<input
type="text"
placeholder="Last Name"
onChange={handleChange}
name="lastName"
value={formData.lastName}
/>
<input
type="email"
placeholder="Email"
onChange={handleChange}
name="email"
value={formData.email}
/>
<textarea
value={formData.comments}
placeholder="Comments"
onChange={handleChange}
name="comments"
/>
<input
type="checkbox"
id="isFriendly"
checked={formData.isFriendly}
onChange={handleChange}
name="isFriendly"
/>
<label htmlFor="isFriendly">Are you friendly?</label>
<br />
<br />

<fieldset>
<legend>Current employment status</legend>

<input
type="radio"
id="unemployed"
/>
<label htmlFor="unemployed">Unemployed</label>
<br />

<input
type="radio"
id="part-time"
/>
<label htmlFor="part-time">Part-time</label>
<br />

<input
type="radio"
id="full-time"
/>
<label htmlFor="full-time">Full-time</label>
<br />

</fieldset>
</form>
)
}
Console
/index.html
-6:00