为什么这里的 react 中的 ref 为 null?
Why the ref in react is null here?
我一直在试验 popmotion pure,因为动画必须使用 "ref" 我正在使用它。
这里,为什么ref(number)的值为null。
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
它return错误为TypeError: Cannot set property 'innerHTML' of null
但是,如果我使用这个文档而不是 null,它工作正常
const number = document.querySelector('#count');
谁能指导我完成。
谢谢
使用
const number = this.count.current;
而不是
const number = this.count.current.querySelector('#count');
我一直在试验 popmotion pure,因为动画必须使用 "ref" 我正在使用它。
这里,为什么ref(number)的值为null。
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
它return错误为TypeError: Cannot set property 'innerHTML' of null
但是,如果我使用这个文档而不是 null,它工作正常
const number = document.querySelector('#count');
谁能指导我完成。 谢谢
使用
const number = this.count.current;
而不是
const number = this.count.current.querySelector('#count');