我正在尝试在 React 中创建一个闪烁器,但是,闪烁器本身以不一致的速率闪烁/渲染,并且表现得有些令人沮丧。请参阅随附的片段。组件安装有什么问题吗?谢谢!
class Blinker extends React.Component {
constructor(props) {
super();
this.state = {
appear: true
}
this.blinker = this.blinker.bind(this);
}
blinker() {
this.setState({appear: !this.state.appear });
}
componentDidMount() {
setInterval(this.blinker, 1000)
}
componentDidUpdate() {
setTimeout(this.blinker, 1000)
}
render() {
return (
<div>
{ (this.state.appear) && "xxx" }
</div>
);
}
}
ReactDOM.render(
<Blinker />,
app
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>