Hooks 11 exercises
Problem

Solving Errors with useEffect

Consider this useTimeout hook that takes in a time in milliseconds:


export const useTimeout = (timerMs: number) => {
useEffect(
() =>
setTimeout(() => {
console.log("Done!");
}, timerMs),
[timerMs],
);
};

It's using a useEffect with a function

Loading exercise

Transcript

0:00 In this exercise, I'm giving you a TypeScript error to solve. This useTimeout hook is a little bit immature. It's using useEffect. We've got this setTimeout function here. What it's doing is it's taking in the timer milliseconds. We're getting an error here.

0:19 We are passing, as the first argument to useEffect, a function that returns the result of setTimeout, but it looks like Type Timeout is not assignable to type void or Destructor.

0:32 Your job is to figure out what's going on here, how we can make this error go away. We also probably need to clear the timeout at some point. Otherwise, it's going to change every time or rerun every time that the timerMs changes. There's a few little jobs here. See if you can find your way to making this hook work and to making this TypeScript error go away. Good luck.