我正在尝试在我的项目中关注此示例,但我无法使其正常工作https://react-slick.neostack.com/docs/example/as-nav-for 我认为原因是:
我认为ref callback是在componentDidMount()之前执行的,但事实并非如此:
我的代码:
Component {
state = {
topNav: null,
bottomNav: null
}
componentDidMount () {
console.log("componentDidMount being called : slider1 : " + this._topSlider + " slider 2 : " + this._bottomSlider)
this.setState({
topNav: this._topSlider,
bottomNav: this._bottomSlider
});
}
render() {
console.log("render being called")
...
<Slider
asNavFor={this.state.bottomNav}
ref={(slider) => {
this._topSlider = slider
console.log("topslider ref being called" + this._topSlider )
}}
>
...
</Slider>
<Slider
asNavFor={this.state.topNav}
ref={(slider) => {
this._bottomSlider = slider
console.log("bottomslider ref being called"+this._bottomSlider)
}}
slidesToShow={3}
swipeToSlide={true}
focusOnSelect={true}
>
...
</Slider>
...
}
这些是日志: