用 antd mobile 的ListView的时候遇到的问题

Stateless function components cannot be given refs. Attempts to access this ref will fail.

 <div>
    <ListView
      ref={ el => this.lv = el }
      dataSource={ this.state.dataSource }
      renderScrollComponent={ () => <MyBody /> }
      renderRow={row}
      onEndReachedThreshold={100}
      onEndReached={this.getList}
      style={{
        height: '500px',
        overflow: 'auto',
      }}
    />
</div>

在使用的时候报这个错  
而且为什么react有时候使用 refs 只能获取组件实例   不能获取真实的dom节点?
阅读 5.7k
1 个回答

这个异常的解决方式是把stateless组件修改为class
但是你引用的是第三方的。所以这个方式应该无效。
所以你不能在ListView组件中用refs来获取数据,只能通过其他方式获取,比如回调。

再次强调,使用react就忘掉真实dom吧。不要尝试获取真实dom,也不要操作真实Dom

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题