首页
Javascript
Html
Css
Node.js
Electron
移动开发
小程序
工具类
服务端
浏览器相关
前端收藏
其他
关于
公司注册

关于react的ref不能使用字符串,eslint提示:Using string literals in ref attributes is deprecated

2020年02月29日 发布 阅读(2622) 作者:Jerman

react的JSX之ref如果使用字符串值 ,eslint检查会提示

  1. Using string literals in ref attributes is deprecated

怎么处理呢?如下中是一种方式

  1. class Update extends Component {
  2. constructor() {
  3. super();
  4. this.container = React.createRef();
  5. }
  6. render(){
  7. // 调用
  8. // 关闭窗口时,恢复列表滚动到顶部
  9. if (this.listContainer) {
  10. if (this.listContainer.current) {
  11. this.listContainer.current.scrollTop = 0;
  12. }
  13. }
  14. return (
  15. <>
  16. <div ref={this.container}>布局</div>
  17. </>
  18. )
  19. }

详细参考官方文档 https://reactjs.org/docs/refs-and-the-dom.html

版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处