I'm trying to change the opacity of an element based on its own position (show it only if its bigger than 500px):
export const Card = ({ pic }) => {
const card = useRef(null);
return (
<li
ref={card}
style={{opacity: 500 > card.current.getBoundingClientRect().y ? 1 : 0}}
>
<img src={pic} />
</li>
);
};
But I got this error: Uncaught TypeError: Cannot read properties of null (reading 'getBoundingClientRect')
And could not figure out how to access its position currectly.
Thanks!