Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6157

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T03:55:08+00:00 2024-11-27T03:55:08+00:00

Intersection Observer using React

  • 60k

Today we are gonna explore how to use the intersection observer API in React and see some useful examples, you can find the code in the following repository, let's begin.


Mozilla web documentation describes the intersection observer API as:

lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount. This way, sites no longer need to do anything on the main thread to watch for this kind of element intersection, and the browser is free to optimize the management of intersections as it sees fit.

In plain english, it allows us to detect when certain elements are visible in our viewport, this only happens when the element meets your desired intersection ratio.

threshold explained

As you can see, if we scroll down the page the intersection ratio will be going up until it meets the designed threshold and that's when the callback function is executed.


Initialization

Constructor

The intersection observer object constructor requires two arguments:

  1. Callback function
  2. Options

Just like that we are ready to see some action but first we need to know what each option means, the options argument is an object with the following values:

Options

  • root: The element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if null.
  • rootMargin: This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections, the options are similar to those of margin in CSS.
  • threshold: Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed, ranges from 0 to 1.0, where 1.0 means every pixel is visible in the viewport.

Using React.js

viewport implementation gif

Now let's see an implementation of the intersection observer API using react.

Basic example of use in react

  1. Start with a reference to the element we want to observe, use the react hook useRef.
  2. Create a state variable isVisible, we are gonna use it to display a message whenever our box is in the viewport.
  3. Declare a callback function that receives an array of IntersectionObserverEntries as a parameter, inside this function we take the first and only entry and check if is intersecting with the viewport and if it is then we call setIsVisible with the value of entry.isIntersecting (true/false).
  4. Create the options object with the same values as the image.
  5. Add the react hook useEffect and create an observer contructor using the callback function and the options we just created before, it's optional in our case but you can return a cleanup function to unobserve our target when the component unmounts.
  6. Set the useRef variable on the element we want to observe. useRef element
  7. Let's add a background and some properties to our HTML elements css styles
  8. It's done, simple and easy!

remember this is just a basic implementation and there are many different ways of doing it.


Hooking it up

in viewport gif 2

Let's now implement the same code we just did before but separating all the logic into a new hook called useElementOnScreen.

hook code 1

  1. Create a new function called useElementOnScreen with the parameter options
  2. Move useRef, useState and the entire useEffect inside our new shiny hook.
  3. Now the only thing missing in our hook is the return statement, we pass isVisible and containerRef as an array.
  4. okay, we are almost there, we just need to call it in our component and see if it works!

hook code 2

  1. Import the recently created hook file into our component.
  2. Initialize it with the options object.
  3. Just like that we are done.

Congratulations we have successfully used the intersection observer API and we even made a hook for it!


Final words

Thanks for reading this, Hopefully it helped someone getting started with the IO API using react, stay safe ❤️!

good bye gif

htmljavascriptreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.