在componentDidMount之前没有调用React引用

时间:2018-04-09 15:28:11

标签: reactjs react-slick react-lifecycle

我正在尝试在我的项目中关注此示例,但我无法使其正常工作https://react-slick.neostack.com/docs/example/as-nav-for 我认为原因是:

  1. 它需要引用两个对象来同步它们
  2. 可以与componentDidMount()中设置的状态同步
  3. 当设置状态时,引用是未定义的
  4. 我认为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>
        ...
    }
    

    这些是日志:

    enter image description here

    类似的问题: componentDidMount called BEFORE ref callback

0 个答案:

没有答案