【发布时间】:2017-12-30 06:32:21
【问题描述】:
想法:我想在登录后显示时间并在注销后隐藏它。程序有效,但注销后我收到警告(见图 3)。所以,我想知道:警告会影响应用程序,如果是,如何解决这个问题。
这是 Clock.jsx:
import React from 'react'
export class Clock extends React.Component {
constructor(props) {
super(props)
this.state = {
date: new Date()
}
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
)
}
componentWillMount() {
clearInterval(this.timerID)
}
tick() {
this.setState({
date: new Date()
})
}
render() {
return (
<div>
<hr />
<p>Current Time: </p>
<p>{this.state.date.toLocaleTimeString()}.</p>
<hr />
</div>
)
}
}
在 index.jsx 中调用该组件:
function Logout(props) {
return (
<div>
<button className="btn btn-danger" onClick={props.onClick}>
Logout
</button>
<Clock />
</div>
)
}
【问题讨论】:
-
当你点击注销时,它会卸载你的
<Logout />组件吗?
标签: javascript reactjs