Reputation: 787
I have a React.useRef in the parent component const portfolioRef = React.useRef(null)
And want to use the current version of that in the child component const parent = portfolioRef.current
. But when I use it in this way, it says cannot read property of current
. Has anyone an idea how to fix this?
export function Portfolio() {
const portfolioRef = React.useRef(null)
return (
<div className={cx(styles.component, styles.scrollWrapper)}>
<div className={styles.topIcon} dangerouslySetInnerHTML={{ __html: arrow }} />
<div ref={portfolioRef} className={styles.scroll}>
<PortfolioItem
title='Article about Kaliber Academie'
text='I wrote an article about my experience at Kaliber'
link='https://medium.com/kaliberinteractive/hoe-technologie-het-hart-van-een-luie-scholier-veranderde-3cd3795c6e33'
linkTekst='See Article' />
</div>
</div>
)
}
function PortfolioItem({ text, title, link, linkTekst, portfolioRef }) {
const portfolioItemRef = React.useRef(null)
React.useEffect(() => {
const element = portfolioItemRef.current
const parent = portfolioRef.current
calculateDistance(parent, element)
}, [portfolioRef])
return (
<div ref={portfolioItemRef} className={styles.componentItem}>
<div className={styles.title}>{title}</div>
<div className={styles.content}>
<div className={styles.text}>{text}</div>
<div className={styles.links}>
<a className={styles.linkTekst} href={link}>{linkTekst} </a>
<div className={styles.linkIcon} dangerouslySetInnerHTML={{
__html:arrow }} />
</div>
</div>
</div>
)
}
function calculateDistance(parent, element) {
const parentRect = parent.getBoundingClientRect()
const parentCenter = parentRect.top + parentRect.height / 2
const elementRect = element.getBoundingClientRect()
const elementCenter = elementRect.top + elementRect.height / 2
const distance = Math.abs(elementCenter - parentCenter)
console.log(distance)
}
Upvotes: 0
Views: 1371
Reputation: 21901
function PortfolioItem({ text, title, link, linkTekst, portfolioRef }) {
const portfolioItemRef = React.useRef(null) //line 1
const element = portfolioItemRef.current //line 2
const parent = portfolioRef.current //line 3
calculateDistance(parent, element)
return (
<div ref={portfolioItemRef} className={styles.componentItem}>
</div>
)
}
when the code execute in order
useRef
nad initiate it to null,null
//Error, because ref is not holding any value at this momentto access the value you have to put the logic inside useRef
React.useEffect(() => {
const element = portfolioItemRef.current
const parent = portfolioRef.current
calculateDistance(parent, element)
}, [])
not tested.
Upvotes: 2
Reputation: 2866
It seems that you don't pass the ref to the child at all. Try to pass it in props
<PortfolioItem
portfolioRef={portfolioRef} {/* here you are passing the ref in props */}
title="Article about Kaliber Academie"
text="I wrote an article about my experience at Kaliber"
link="https://medium.com/kaliberinteractive/hoe-technologie-het-hart-van-een-luie-scholier-veranderde-3cd3795c6e33"
linkTekst="See Article"
/>;
Upvotes: 2