scrimba
What's new in React 19
Form action
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 action
Expand for more info
index.jsx
run
preview
console
import ReactDOM from "react-dom/client"
import { useState } from "react"
import { updateNameInDB } from "./api"

function App() {
const [input, setInput] = useState("")
const [name, setName] = useState(
() => JSON.parse(localStorage.getItem("name")) || "Anonymous user"
)

function handleChange(event) {
setInput(event.target.value)
}

async function handleSubmit(event) {
event.preventDefault()
try {
const newName = await updateNameInDB(input)
setName(newName)
setInput("")
} catch (error) {
console.error(error.message)
}
}

return (
<>
<p className="username">
Current user: <span>{name}</span>
</p>
<form onSubmit={handleSubmit}>
<input
type="text"
value={input}
onChange={handleChange}
required
/>
<button type="submit">Update</button>
</form>
</>
)
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />)
Console
/index.html
-7:30