Add Interval Time as a Hook Parameter
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Lesson Transcript
[00:00 - 02:26] So far the interval used by our use interval calls in both title change effect and in fav icon change effect is hard-coded at 500 milliseconds We should instead have this be a parameter that can be passed into our hook So let's first add it to the signature of use please stay And we'll just call it interval and we know it's a type number We then need to pass this through to both the use title change effect and The use fav icon change effect And we'll swap out the 500 here or interval You can save this and the same for use fav icon change effect And as a quick check let's rebuild the hook Move into the example folder and in app.tsx Let's keep the configuration from last lesson, but we'll pass an interval of 3000 milliseconds or three seconds Now let's run the example And now we see whenever we open a tab The change is a little bit slower, it's not a half second now, but every three seconds So it looks like our new interval parameter works just fine So in summary we added a new interval parameter to use please stay we passed this parameter on to both the use title change effect and use fav icon change effect and in both of those hooks instead of the hard-coded 500 milliseconds We simply forwarded on the interval In the next lesson, we'll add yet another parameter which will allow the user to always play the animation and not just when the page focus is lost
This lesson preview is part of the Master Custom React Hooks with TypeScript course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Master Custom React Hooks with TypeScript, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
![Thumbnail for the \newline course Master Custom React Hooks with TypeScript](/_next/image?url=https%3A%2F%2Fs3.amazonaws.com%2Fassets.fullstack.io%2Fn%2F20240702111139809_ChrisFrewin_BuildandPublishanOpen-SourceReactHooksLibrarytonpm_1450.67x816_CoverImage.png&w=3840&q=75)