Pie Logo
Quiz: useRef
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
Next.js
Advanced React
Pure Components - memo
Lifecycle - useEffect
Expensive? - useMemo
DOM Interactions - useRef
Exercise: useRef
Quiz: useRef
Error Boundaries
Context
Leaderboards
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: useRef
    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
    Next.js
    Advanced React
    Pure Components - memo
    Lifecycle - useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Exercise: useRef
    Quiz: useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    Do you know useEffect?
    by Tomas Trescak· DOM Interactions - useRef

    QUESTION 1

    What is the primary use case of useRef in React?


    QUESTION 2

    Which of the following statements about useRef is true?


    QUESTION 3

    What will happen if you update the .current property of a useRef object?


    QUESTION 4

    How can you use useRef to track the previous value of a prop or state?


    QUESTION 5

    Consider the following component:

    import React, { useRef, useEffect } from "react";
    
    const DomRefDemo = () => {
      const inputRef = useRef(null);
    
      useEffect(() => {
        // Focus the input field on component mount
        inputRef.current.focus();
      }, []);
    
      return (
        <div>
          <h1>useRef Example: DOM Access</h1>
          <input ref={inputRef} type="text" placeholder="I will be focused on load" />
        </div>
      );
    };
    
    export default DomRefDemo;


    In the DomRefDemo component, the input field automatically gains focus when the component loads. What would happen if you Removed the useEffect hook entirely from the code?


    QUESTION 6

    Consider the following component:

    import React, { useRef, useState } from "react";
    
    const MutableRefDemo = () => {
      const renderCount = useRef(1);
      const [count, setCount] = useState(0);
    
      // Increment renderCount on each re-render
      renderCount.current += 1;
    
      return (
        <div>
          <h1>useRef Example: Mutable Values</h1>
          <p>Component Re-rendered: {renderCount.current} times</p>
          <p>State Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment Count</button>
        </div>
      );
    };
    
    export default MutableRefDemo;

    Imagine you’re building a counter app based on the MutableRefDemo example. You want to display a message "Rendered more than 5 times!" when renderCount.current exceeds 5. Where should you check this condition?


    QUESTION 7

    Consider the following component:

    import React, { useEffect, useRef, useState } from "react";
    
    const PreviousValueDemo = () => {
      const [count, setCount] = useState(0);
      const prevCount = useRef(count);
    
      useEffect(() => {
        prevCount.current = count; // Update the ref value after render
      }, [count]);
    
      return (
        <div>
          <h1>useRef Example: Preserve Previous State</h1>
          <p>Current Count: {count}</p>
          <p>Previous Count: {prevCount.current}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
        </div>
      );
    };
    
    export default PreviousValueDemo;

    In the PreviousValueDemo component, the prevCount ref tracks the previous value of count. If count starts at 0 and the user clicks "Increment" twice, what will the JSX display for Current Count and Previous Count?


    QUESTION 8

    Suppose you modify the MutableRefDemo code from Question 6, to reset renderCount.current to 0 when the user clicks a new "Reset" button. What potential issue might arise?


    QUESTION 9

    A developer uses useRef to track how many times a button is clicked without displaying it in the UI. Later, they decide to log this value to the console when another button is clicked. Which of these approaches would work based on the useRef behavior?

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

    Do you know useEffect?
    by Tomas Trescak· DOM Interactions - useRef

    QUESTION 1

    What is the primary use case of useRef in React?


    QUESTION 2

    Which of the following statements about useRef is true?


    QUESTION 3

    What will happen if you update the .current property of a useRef object?


    QUESTION 4

    How can you use useRef to track the previous value of a prop or state?


    QUESTION 5

    Consider the following component:

    import React, { useRef, useEffect } from "react";
    
    const DomRefDemo = () => {
      const inputRef = useRef(null);
    
      useEffect(() => {
        // Focus the input field on component mount
        inputRef.current.focus();
      }, []);
    
      return (
        <div>
          <h1>useRef Example: DOM Access</h1>
          <input ref={inputRef} type="text" placeholder="I will be focused on load" />
        </div>
      );
    };
    
    export default DomRefDemo;


    In the DomRefDemo component, the input field automatically gains focus when the component loads. What would happen if you Removed the useEffect hook entirely from the code?


    QUESTION 6

    Consider the following component:

    import React, { useRef, useState } from "react";
    
    const MutableRefDemo = () => {
      const renderCount = useRef(1);
      const [count, setCount] = useState(0);
    
      // Increment renderCount on each re-render
      renderCount.current += 1;
    
      return (
        <div>
          <h1>useRef Example: Mutable Values</h1>
          <p>Component Re-rendered: {renderCount.current} times</p>
          <p>State Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment Count</button>
        </div>
      );
    };
    
    export default MutableRefDemo;

    Imagine you’re building a counter app based on the MutableRefDemo example. You want to display a message "Rendered more than 5 times!" when renderCount.current exceeds 5. Where should you check this condition?


    QUESTION 7

    Consider the following component:

    import React, { useEffect, useRef, useState } from "react";
    
    const PreviousValueDemo = () => {
      const [count, setCount] = useState(0);
      const prevCount = useRef(count);
    
      useEffect(() => {
        prevCount.current = count; // Update the ref value after render
      }, [count]);
    
      return (
        <div>
          <h1>useRef Example: Preserve Previous State</h1>
          <p>Current Count: {count}</p>
          <p>Previous Count: {prevCount.current}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
        </div>
      );
    };
    
    export default PreviousValueDemo;

    In the PreviousValueDemo component, the prevCount ref tracks the previous value of count. If count starts at 0 and the user clicks "Increment" twice, what will the JSX display for Current Count and Previous Count?


    QUESTION 8

    Suppose you modify the MutableRefDemo code from Question 6, to reset renderCount.current to 0 when the user clicks a new "Reset" button. What potential issue might arise?


    QUESTION 9

    A developer uses useRef to track how many times a button is clicked without displaying it in the UI. Later, they decide to log this value to the console when another button is clicked. Which of these approaches would work based on the useRef behavior?

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

    Pie Logo
    Quiz: useRef
    by Tomas Trescak
    Pie Logo
    Quiz: useRef
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Lifecycle - useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Exercise: useRef
    Quiz: useRef
    Error Boundaries
    Context
    Leaderboards
    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
    Next.js
    Advanced React
    Pure Components - memo
    Lifecycle - useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Exercise: useRef
    Quiz: useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics