Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React sticky hook

import React, { Fragment, useEffect, useRef, useState } from 'react';
import Sticky from './sticky';

export default () => {
  const [isSticky, setSticky] = useState(false);
  const ref = useRef(null);
  const handleScroll = () => {
    if (ref.current) {
      setSticky(ref.current.getBoundingClientRect().top <= 0);
    }
  };

  useEffect(() => {
    window.addEventListener('scroll', handleScroll);

    return () => {
      window.removeEventListener('scroll', () => handleScroll);
    };
  }, []);

  return (
    <Fragment>
      <p>Lorem ipsum...</p>
      <div className={`sticky-wrapper${isSticky ? ' sticky' : ''}`} ref={ref}>
        <Sticky />
      </div>
      <p>Lorem ipsum...</p>
    </Fragment>
  );
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: get search value from reacr route 
Javascript :: npm remopve existing files 
Javascript :: react native section list sort by alphabet 
Javascript :: Get the Middle Character 
Javascript :: prisma where not in array 
Javascript :: javascript how-do-i-check-whether-a-checkbox-is-checked-in-jquery 
Javascript :: Repeat a String Repeat a String-Javascript 
Javascript :: javascript seconds after input 
Javascript :: self-invoking function 
Javascript :: install node specific version ubuntu 
Javascript :: use the whatwg url api instead 
Javascript :: multiple image upload react 
Javascript :: javascript assign multiple variables to same value ES6 
Javascript :: angular json and cli json file 
Javascript :: js reverse 
Javascript :: schema in mongoose 
Javascript :: npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. 
Javascript :: side effect, useEffect, return 
Javascript :: to array javascript 
Javascript :: how to access node js server from another computer 
Javascript :: exponential javascript 
Javascript :: Import A Function From A Module In JavaScript 
Javascript :: environment variable to debug knex 
Javascript :: default in javascript 
Javascript :: class component in react 
Javascript :: Angular patchValue dynamically 
Javascript :: js alert with multiple buttons 
Javascript :: ajax add custom header 
Javascript :: Animated Sticky Header 
Javascript :: calling anonymous function while declaring it 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =