0

I'm trying to follow this example in my project but I couldn't make it work https://react-slick.neostack.com/docs/example/as-nav-for I think the reason is:

  1. It needs a ref to both objects to sync them
  2. It is supossed to be synced with the state setted in componentDidMount()
  3. When the state is setted the refs are undefined

I thought ref callback are suppossed to execute before componentDidMount(), but that's not the case:

Bits of my code:

 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>
    ...
}

these are the logs:

enter image description here

Similar Question: componentDidMount called BEFORE ref callback

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Jumper
  • 151
  • 1
  • 14
  • That seems strange. Could you please provide the complete example? Are you sure the omitted code don't return before rendering the sliders? – Sulthan Apr 09 '18 at 15:38
  • I created a [codesandbox](https://codesandbox.io/s/kkwr6rjjjv) with your code and it works fine calling first the `ref`s and then `componentDidMount()`. The reason for this behaviour must be in the code that you omitted. Please add the missing parts. – trixn Apr 09 '18 at 15:51

0 Answers0