Answer the question
In order to leave comments, you need to log in
How to pass ref to child element via props?
There is this component:
class Services extends React.Component {
constructor(props) {
super(props);
this.slider = React.createRef();
}
render() {
const sliderOptions = {
dots: false,
infinite: true,
speed: 500,
slidesToShow: 2,
slidesToScroll: 1,
arrows: false,
}
return(
<section className={style.services}>
<div className={style.servicesLeft}>
<Accordion />
<SliderArrows slider={this.slider} />
</div>
<div className={style.servicesRight}>
<Slider ref={this.slider} {...sliderOptions}>
<ServiceItem />
<ServiceItem />
</Slider>
</div>
</section>
)
}
}
(<Slider></Slider>
) to the arrow( <SliderArrows />
) component via props. The problem is that in the component , the <SliderArrows />
passed reference is null, i.e. it is not transmitted. After reading the documentation, I came across forwardRef, but I didn’t fully understand how it works and whether it would even help in my cases import React from 'react';
function ForwardRefSlider(Slider) {
return React.forwardRef((props, ref) =>{
<Slider />
})
}
<SliderArrows />
component is not clear.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question