import React from "react";
import { useInView } from "react-intersection-observer";
type PhotoProps = {
name: string;
imgUrl: string;
};
export default function Photo({ name, imgUrl }: PhotoProps) {
const { ref, inView } = useInView({
threshold: 0.5,
});
return (
<div ref={ref} className="photo--card">
{inView ? <img src={imgUrl} alt={name} /> : <div className="Skeleton"></div>}
<h4>{name}</h4>
</div>
);
}
the error is Type (node?: (Element | null)) => void is not assignable to type LegacyRef<HTMLDivElement> | undefined
i tried to specify ref as HTMLDivElement
, but ref stops working then, also you need to destructure these components separatly in order to do that. Im new to typescript, so im definetely doing smth wrong, but neither google, nor chatGPT couldn`t solve this problen
2
Answers
Try to cast the
ref
variable toMutableRefObject<HTMLDivElement>
.Hope it could help.
In most cases with useInView from react-intersection-observer, using React.RefObject is sufficient because you typically don’t need to modify the ref object itself, only to use it to interact with the DOM element it refers to.