import React, { Component } from 'react';
import ReactDOM from 'react-dom';
export default class Game extends Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
this.check = this.check.bind(this);
}
drawBackground() {
console.log("worked");
}
check () {
this.myRef.current.innerHTML = "Testing";
{this.drawBackground()}
}
render() {
return (
<div>
<h1 ref={this.myRef} id="foo">bar</h1>
{this.check()}
</div>
);
}
}
我需要访问函数中的text
内部h1
标记check
,但出现此错误 Reactjs: Uncaught TypeError: Cannot set property 'innerHTML' of null。我遵循了文档。我错过了什么吗?