scrimba
Frontend Career Path
React basics
Meme generator
Form state object practice
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

AboutCommentsNotes
Form state object practice
Expand for more info
Form.js
run
preview
console
import React from "react"

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

/**
* Challenge: add an email field/state to the form
*/

console.log(formData)

function handleChange(event) {
setFormData(prevFormData => {
return {
...prevFormData,
[event.target.name]: event.target.value
}
})
}

return (
<form>
<input
type="text"
placeholder="First Name"
onChange={handleChange}
name="firstName"
/>
<input
type="text"
placeholder="Last Name"
onChange={handleChange}
name="lastName"
/>
</form>
)
}
Console
{firstName:
""
, lastName:
""
}
,
{firstName:
"a"
, lastName:
""
}
,
{firstName:
"as"
, lastName:
""
}
,
{firstName:
"asd"
, lastName:
""
}
,
{firstName:
"asdf"
, lastName:
""
}
,
{firstName:
"asdfg"
, lastName:
""
}
,
{firstName:
"asdfg"
, lastName:
"q"
}
,
{firstName:
"asdfg"
, lastName:
"qw"
}
,
{firstName:
"asdfg"
, lastName:
"qwe"
}
,
{firstName:
"asdfg"
, lastName:
"qwer"
}
,
{firstName:
"asdfg"
, lastName:
"qwert"
}
,
/index.html
-1:49