首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么react中的ref在这里为空?

为什么react中的ref在这里为空?
EN

Stack Overflow用户
提问于 2019-05-15 18:45:22
回答 1查看 172关注 0票数 2

我一直在实验流行运动纯,因为动画将不得不使用“参考”,我正在使用它。

这里,为什么ref(number)的值是空的。

代码语言:javascript
复制
import React from 'react'
import {
  styler,
  tween,
  merge,
  action,
  easing
} from "popmotion";

class Demo extends React.Component {
  constructor(props) {
    super(props)
    this.count = React.createRef();
  }

  componentDidMount() {
    const number = this.count.current.querySelector('#count');
    const updateCounter = (v) => {
      console.log(v)
      return (number.innerHTML = v)
    }

    tween({
      from: 0,
      to: 300,
      flip: Infinity,
      duration: 4000
    }).start(updateCounter);
  }

  render() {

    return ( 
      <div>
        <p  ref={this.count} id='count'></p>
        <div id="ball"></div>
      </div>
    )
  }
}

export default Demo

它作为TypeError: Cannot set property 'innerHTML' of null返回错误

但是,如果我使用这个文档而不是null,它就可以正常工作了

代码语言:javascript
复制
 const number = document.querySelector('#count');

有人能带我过去吗。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-15 18:55:41

使用

代码语言:javascript
复制
const number = this.count.current;

而不是

代码语言:javascript
复制
const number = this.count.current.querySelector('#count');

见实例化

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56155845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档