Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    State and events · Lesson 9 of 21

    Forms and controlled inputs

    value and onChange, submitting, validation, and when to go uncontrolled.

    10 minutes2 games

    A controlled input takes its value from state: value={name}, and every keystroke goes through onChange, which updates the state. State becomes the single source of truth, so you can validate, transform and display the value anywhere.

    React
    function App() {
      const [form, setForm] = useState({ email: "", level: "beginner", agree: false })
      const [sent, setSent] = useState(null)
    
      const update = (e) => {
        const { name, value, type, checked } = e.target
        setForm({ ...form, [name]: type === "checkbox" ? checked : value })
      }
      const valid = form.email.includes("@") && form.agree
    
      function submit(e) {
        e.preventDefault()               // no page reload
        setSent(form)
      }
    
      return (
        <form onSubmit={submit} style={{ display: "grid", gap: 8, maxWidth: 280 }}>
          <input name="email" value={form.email} onChange={update} placeholder="Email" />
          <select name="level" value={form.level} onChange={update}>
            <option value="beginner">Beginner</option>
            <option value="advanced">Advanced</option>
          </select>
          <label><input type="checkbox" name="agree" checked={form.agree} onChange={update} /> I agree</label>
          <button disabled={!valid}>Join</button>
          {sent && <p>Welcome, {sent.email} ({sent.level})!</p>}
        </form>
      )
    }

    The user types a character

    onChange(e) → setForm

    re-render with the new state

    the input shows value={form.email}

    This loop runs on every keystroke.

    Important

    Setting value without onChange locks the input: the user types and nothing changes, and React warns you. If the initial value is undefined and later becomes text, React warns that the input switched from uncontrolled to controlled, so always start with "".

    Uncontrolled inputs (no value; read the data on submit via FormData or useRef) are simpler and faster for large forms that don't need live validation. Libraries like React Hook Form build on this idea.

    Complete the code

    Complete it so the input is controlled.

    function App() {
      const [name, setName] = useState("")
      return <input ={name} onChange={(e) => setName(e.target.)} />
    }

    Interview question

    What's the difference between controlled and uncontrolled components, and when do you use each?

    Exercise

    A name input, and below it a <p> showing Hello, NAME as you type, or Type your name when empty. The checker types Laila.

    The page should show: Hello, Laila

    React
    function App() {
      return (
        <div>
          <input placeholder="Name" />
          <p>Type your name</p>
        </div>
      )
    }

    Learn more from

    • react.dev: <input> referencereact.dev
    • react.dev: Reacting to input with statereact.dev
    • MDN: FormDatadeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousObjects and arrays in stateNextLifting state up
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.