Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

component rerendering when view port comes

import { useState, useEffect } from "react";

const OPTIONS = {
  root: null,
  rootMargin: "0px 0px 0px 0px",
  threshold: 0,
};

const useIsVisible = (elementRef) => {
  const [isVisible, setIsVisible] = useState(false);

  useEffect(() => {
    if (elementRef.current) {
      const observer = new IntersectionObserver((entries, observer) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            setIsVisible(true);
            observer.unobserve(elementRef.current);
          }
        });
      }, OPTIONS);
      observer.observe(elementRef.current);
    }
  }, [elementRef]);

  return isVisible;
};

export default useIsVisible;
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript check if array has at least one true value 
Javascript :: pnpm tailwind react 
Javascript :: javascript protect object with proxy 
Javascript :: var logEvenNums = function(num) {}; 
Javascript :: Backbone Template 
Javascript :: wait untill 2 
Javascript :: react : calling APIs after render w error message 
Javascript :: Backbone With Express 
Javascript :: toISOString() in electron 
Javascript :: Populate a Select Dropdown List using JSON 
Javascript :: Example Vuex Store 
Javascript :: How to Check if an Item is in an Array in JavaScript Using Array.includes() Starting From a Specified Index 
Javascript :: how to receive form data in node js 
Javascript :: js onclick add table row 
Javascript :: number of factors 
Javascript :: convert jquery to javascript converter online tool 
Javascript :: json stringify without quotes 
Javascript :: fs.writefile promise 
Javascript :: open json javascript 
Javascript :: events node.js 
Javascript :: JavaScript (rhino 1.7.9) sample 
Javascript :: .net core json store data type in model oracle 
Javascript :: how dynamique pseudo element in react 
Javascript :: javascript Accessing Object Methods 
Javascript :: javascript WeakSets Are Not iterable 
Javascript :: JavaScript Code Blocks 
Javascript :: card types regex 
Javascript :: OpenTok Create Session 
Javascript :: maximum product of word lengths leetcode solution 
Javascript :: phaser mixed animation 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =