javascript - 从“扩展组件”更改为“createReactClass”
问题描述
我在 react 中实现了以下计时器,它使用“扩展组件”的旧实现方式。我仍在为如何使用“createReactClass”来实现这一点而苦苦挣扎。应该如何适应?
import React, { Component } from 'react'
class Timer extends Component {
constructor (props) {
super(props)
this.state = {
count: 1
}
}
render () {
const {count} = this.state
return (
<div>
<h1>Current Count: {count}</h1>
</div>
)
}
// setInterval
// clearInterval
componentDidMount () {
const {startCount} = this.props
this.setState({
count: startCount
})
this.doIntervalChange()
}
doIntervalChange = () => {
this.myInterval = setInterval(() => {
this.setState(prevState => ({
count: prevState.count - 1
}))
}, 1000)
}
componentWillUnmount () {
clearInterval(this.myInterval)
}
}
export default Timer
提前致谢!
解决方案
推荐阅读
- c# - asp.net MVC 活动账户
- continuous-integration - TeamCity 构建报告选项卡起始页
- php - 使用 PHP 解析 fasta 文件
- c# - 当 FocusSearch 中的下一个控件无法聚焦于 Telerik 数据表单时,应用程序崩溃
- python - 我怎样才能腌制一个类定义?
- php - 在会话 Laravel 中更改值多维数组
- c - 在 C 中搜索 int*
- javascript - toggleClass jQuery 类赋值
- android - 在滚动视图中使用寻呼机进行缩放(类似于 instagram 的功能)
- android - 弹出窗口 Recyclerview 中的 Onclick 监听器