我正在尝试在我的 React 项目中制作自动文本动画。我可以让它在 VanillaJS 中工作,但我不知道如何在 React 中做到这一点。(我是 React 的初学者。)
import React, { Component } from 'react'
class AutoTextSection extends Component {
writeText = () => {
let idx = 1
const text = "This is the text sentence."
document.getElementById('auto-text').textContent = text.slice(0, idx)
idx++
if (idx > document.getElementById('auto-text').length) {
idx = 1
}
setTimeout(this.writeText, 1000)
}
render() {
return (
<section id="auto-text-sec">
<h2 className="text-light" id="auto-text">
{this.writeText()}
</h2>
</section>
)
}
}
只是我能看到第一个字母。然后它向我抛出了这个错误:TypeError:无法将属性'textContent'设置为null。