【发布时间】:2023-04-09 07:05:01
【问题描述】:
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>
);
}
}
我需要在check 函数中访问h1 标记内的text,但我收到此错误 Reactjs: Uncaught TypeError: Cannot set property 'innerHTML' of null。我遵循了文档。我错过了什么吗?
【问题讨论】:
标签: javascript reactjs dom react-component