Pie Logo
Quiz: Forms and Events
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
JSX and Components
Props
Assignment 1.2: Front End
CSS Styles
useState and Hooks
Conditional Rendering
Lists
Forms and Events
Exercise: Forms
Quiz: Forms and Events
Storybook
Assignment 1.3: Bonus - Backend
Leaderboards
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: Forms and Events
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Exercise: Forms
    Quiz: Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    Forms and Events
    by Tomas Trescak· Forms and Events

    QUESTION 1

    In the FeedbackForm example below, a button click updates the message state to "Thank you for your feedback!". Imagine a student accidentally removes the onClick handler from the button. What will happen when the user clicks the button?

    function FeedbackForm() {
      const [message, setMessage] = useState("");
      const handleClick = (e) => {
        setMessage("Thank you for your feedback!");
      };
      return (
        <div>
          <button>Submit Feedback</button>
          <p>{message}</p>
        </div>
      );
    }
    


    QUESTION 2

    Examine the uncontrolled form below. A student wants to display the input value in real-time as the user types (e.g., in a <p> tag). Why doesn’t this work as expected with the current code?

    function UncontrolledForm() {
      const inputRef = useRef();
      const handleSubmit = () => {
        alert(`Input Value: ${inputRef.current.value}`);
      };
      return (
        <div>
          <input id="title" type="text" ref={inputRef} />
          <button onClick={handleSubmit}>Submit</button>
        </div>
      );
    }
    


    QUESTION 3

    Consider this controlled form with validation. If the user types nothing in the title field and clicks away (blurring the input), what will happen?

    function Form() {
      const [formState, setFormState] = useState({ title: "" });
      const [errors, setErrors] = useState({ title: "" });
      const validate = () => {
        setErrors({ title: formState.title === "" ? "Title is required" : "" });
      };
      const updateFormState = (e) => {
        setFormState({ title: e.currentTarget.value });
      };
      return (
        <div>
          <input name="title" value={formState.title} onChange={updateFormState} onBlur={validate} />
          {errors.title && <div style={{ color: "red" }}>{errors.title}</div>}
        </div>
      );
    }
    


    QUESTION 4

    In the checkbox example below, a student modifies the onChange handler to log the checkbox state. Which of these modifications will correctly log whether the checkbox is checked?

    function Form() {
      const [formState, setFormState] = useState({ agree: false });
      return (
        <div>
          <input
            id="agree"
            name="agree"
            checked={formState.agree}
            type="checkbox"
            onChange={(e) => setFormState({ agree: e.currentTarget.checked })}
          />
          <label htmlFor="agree">I agree</label>
        </div>
      );
    }
    


    QUESTION 5

    A student builds a form with a select input as shown below. After selecting "BMW," they notice the displayed value doesn’t update. What’s the issue, and how can they fix it?

    function Form() {
      const [formState, setFormState] = useState({ car: "" });
      return (
        <div>
          <select id="car" name="car" value={formState.car}>
            <option value="">Please Select</option>
            {["Mazda", "Toyota", "BMW"].map((t) => (
              <option key={t} value={t}>{t}</option>
            ))}
          </select>
          <p>Selected car: {formState.car}</p>
        </div>
      );
    }
    

    This content is only available to registered users.
    Please register at the the home page.

    Forms and Events
    by Tomas Trescak· Forms and Events

    QUESTION 1

    In the FeedbackForm example below, a button click updates the message state to "Thank you for your feedback!". Imagine a student accidentally removes the onClick handler from the button. What will happen when the user clicks the button?

    function FeedbackForm() {
      const [message, setMessage] = useState("");
      const handleClick = (e) => {
        setMessage("Thank you for your feedback!");
      };
      return (
        <div>
          <button>Submit Feedback</button>
          <p>{message}</p>
        </div>
      );
    }
    


    QUESTION 2

    Examine the uncontrolled form below. A student wants to display the input value in real-time as the user types (e.g., in a <p> tag). Why doesn’t this work as expected with the current code?

    function UncontrolledForm() {
      const inputRef = useRef();
      const handleSubmit = () => {
        alert(`Input Value: ${inputRef.current.value}`);
      };
      return (
        <div>
          <input id="title" type="text" ref={inputRef} />
          <button onClick={handleSubmit}>Submit</button>
        </div>
      );
    }
    


    QUESTION 3

    Consider this controlled form with validation. If the user types nothing in the title field and clicks away (blurring the input), what will happen?

    function Form() {
      const [formState, setFormState] = useState({ title: "" });
      const [errors, setErrors] = useState({ title: "" });
      const validate = () => {
        setErrors({ title: formState.title === "" ? "Title is required" : "" });
      };
      const updateFormState = (e) => {
        setFormState({ title: e.currentTarget.value });
      };
      return (
        <div>
          <input name="title" value={formState.title} onChange={updateFormState} onBlur={validate} />
          {errors.title && <div style={{ color: "red" }}>{errors.title}</div>}
        </div>
      );
    }
    


    QUESTION 4

    In the checkbox example below, a student modifies the onChange handler to log the checkbox state. Which of these modifications will correctly log whether the checkbox is checked?

    function Form() {
      const [formState, setFormState] = useState({ agree: false });
      return (
        <div>
          <input
            id="agree"
            name="agree"
            checked={formState.agree}
            type="checkbox"
            onChange={(e) => setFormState({ agree: e.currentTarget.checked })}
          />
          <label htmlFor="agree">I agree</label>
        </div>
      );
    }
    


    QUESTION 5

    A student builds a form with a select input as shown below. After selecting "BMW," they notice the displayed value doesn’t update. What’s the issue, and how can they fix it?

    function Form() {
      const [formState, setFormState] = useState({ car: "" });
      return (
        <div>
          <select id="car" name="car" value={formState.car}>
            <option value="">Please Select</option>
            {["Mazda", "Toyota", "BMW"].map((t) => (
              <option key={t} value={t}>{t}</option>
            ))}
          </select>
          <p>Selected car: {formState.car}</p>
        </div>
      );
    }
    

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: Forms and Events
    by Tomas Trescak
    Pie Logo
    Quiz: Forms and Events
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Exercise: Forms
    Quiz: Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Lists
    Forms and Events
    Exercise: Forms
    Quiz: Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics