2

I have a useFadein hook

import { useRef, useEffect } from 'react';

export const useFadeIn = (delay = 0) => {
  const elementRef = useRef<HTMLElement>(null);

  useEffect(() => {
    if (!elementRef.current) return
    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting) {
          setTimeout(() => {
            if (!elementRef.current) return
            elementRef.current.classList.add('fade-in');
          }, delay);
        } else {
          if (!elementRef.current) return
          elementRef.current.classList.remove('fade-in');
        }
      },
      { rootMargin: '0px 0px -50% 0px' }
    );
    observer.observe(elementRef.current);
  }, [delay]);

  return { ref: elementRef };
};

I use the hook in my component

"use client"
import styles from "@components/Intro/Intro.module.scss"
import { useFadeIn } from "@hooks/useFadeIn"


const Intro = () => {
  const fadeInTitle = useFadeIn(200);
  const fadeInIntro = useFadeIn(400);
  const fadeInSocial = useFadeIn(600);

  return (
    <section className={styles.wrapper} >
      <h2 {...fadeInTitle}>Test</h2>
      <p {...fadeInIntro}>Test</p>
      <ul {...fadeInSocial} className={styles.socialLinks}>
        <li>
          <a href="#" target="_blank">Test</a>
        </li>
      </ul>
    </section>
  )
}

export { Intro }

But I get the following type errorenter image description here

If I replace HTMLElement with HTMLHeadingElement

const elementRef = useRef<HTMLHeadingElement>(null);

then a similar error with HTMLUListElement shows. If I use

const elementRef = useRef<HTMLHeadingElement | HTMLUListElement>(null);

Then both type errors show.

How can I solve the type error?

2

1 Answer 1

1

You need to retype your custom hook with generic type.

Working example in codesanbox example

export const useFadeI = <T extends HTMLElement>(delay = 0) => {
  const ref = useRef<T | null>(null);

  useEffect(() => {
    const node = ref.current;
    if (!node) return;

    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting) {
          setTimeout(() => {
            node.classList.add("fade-in");
          }, delay);
        } else {
          node.classList.remove("fade-in");
        }
      },
      { rootMargin: "0px 0px -50% 0px" }
    );
    observer.observe(node);
  }, [delay]);

  return { ref };
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.