Exercise: useEffect
by Tomas Trescak· Advanced React

0 / 3310 XP

Objective:

Create a React component that implements a timer. The timer should start when the component mounts, update every second, and stop updating when the component is unmounted. The goal is to practice how to use useEffect for side effects like setting up intervals and cleaning them up.

Starter Code:

import React, { useState, useEffect } from 'react';

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    // Set up the timer logic here.
    return () => {
      // Clean up the timer here when the component unmounts.
    };
  }, []); // Add appropriate dependencies.

  return (
    <div>
      <h1>Timer: {seconds} seconds</h1>
    </div>
  );
}

export default Timer

Instructions:

  1. Set up an interval inside the useEffect hook to update the seconds state every second.
  2. Clean up the interval in the return function of useEffect to avoid memory leaks when the component unmounts.
  3. Test your component to ensure the timer starts when the component mounts and stops correctly when it unmounts.

Expected Behavior:

  • When the Timer component is rendered, the timer should start counting.
  • If the component is removed (e.g., through conditional rendering), the interval should stop.

 

OBJECTIVE: Create a React component that implements a timer. The timer should start when the component mounts, update every second, and stop updating when the component is unmounted. The goal is to practice how to use useEffect for side effects like setting up intervals and cleaning them up. STARTER CODE: import React, { useState, useEffect } from 'react'; function Timer() { const [seconds, setSeconds] = useState(0); useEffect(() => { // Set up the timer logic here. return () => { // Clean up the timer here when the component unmounts. }; }, []); // Add appropriate dependencies. return ( <div> <h1>Timer: {seconds} seconds</h1> </div> ); } export default Timer -------------------------------------------------------------------------------- INSTRUCTIONS: 1. Set up an interval inside the useEffect hook to update the seconds state every second. 2. Clean up the interval in the return function of useEffect to avoid memory leaks when the component unmounts. 3. Test your component to ensure the timer starts when the component mounts and stops correctly when it unmounts. -------------------------------------------------------------------------------- EXPECTED BEHAVIOR: * When the Timer component is rendered, the timer should start counting. * If the component is removed (e.g., through conditional rendering), the interval should stop.  

Discuss with Others
Ask questions, share your thoughts, and discuss with other learners

Join the discussion to ask questions, share your thoughts, and discuss with other learners
Loading...
Ready ...
Content Locked
This content is only available to registered users.
Please register at the the home page.
Setup
React Fundamentals
10 points
Next.js
10 points
Advanced React
Databases
10 points
React Hooks
Authentication and Authorisation
10 points
APIs
CI/CD and DevOps
Testing React
Advanced Topics