課程
/前端開發
/React.JS
/基于實例的 React16 傻瓜課程
照著老師一模一樣寫的
怎么我的時間不變化,定時器沒作用
2018-10-09
源自:基于實例的 React16 傻瓜課程 3-3
正在回答
拼寫錯誤 componentDidMont 應該是 componentDidMount, 下次注意歐
qq_嶸歸_0 提問者
張軒 回復 qq_嶸歸_0 提問者
qq_嶸歸_0 提問者 回復 張軒
代碼貼來看看
import React, { Component } from 'react';import Clock from './Clock';class App extends Component {render() {return (<div><Clock /></div>);}}export default App;
import React from 'react';
class Clock extends React.Component{
constructor(props){
super(props);
this.state={
date:new Date()
}
componentDidMont(){
this.timer=setInterval( ()=>{
this.setState({
date: new Date()
})
}, 1000)
componentDidUpdate(currentProps,currentState){
console.log(currentState)
componentWillUnmount(){
clearInterval(this.timer)
render(){
return(
<h1>{this.state.date.toLocaleTimeString()}</h1>
)
?}
?export default Clock;
張軒
hao
舉報
通過一系列簡單有趣的例子,讓你了解 React16 的基本概念和新特性
2 回答console的12能定時打印,就是時間不變,為什么呢
1 回答問視頻時間
1 回答我看其它視頻時,沒有安裝create-react-app這一步操作
2 回答什么時候用受控組件,什么時候用非受控組件?
2 回答為什么使用state的時候必須使用constructor的方式
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-10-10
拼寫錯誤 componentDidMont 應該是 componentDidMount, 下次注意歐
2018-10-09
代碼貼來看看
2018-10-09
import React, { Component } from 'react';
import Clock from './Clock';
class App extends Component {
render() {
return (
<div>
<Clock />
</div>
);
}
}
export default App;
2018-10-09
import React from 'react';
class Clock extends React.Component{
constructor(props){
super(props);
this.state={
date:new Date()
}
}
componentDidMont(){
this.timer=setInterval( ()=>{
this.setState({
date: new Date()
})
}, 1000)
}
componentDidUpdate(currentProps,currentState){
console.log(currentState)
}
componentWillUnmount(){
clearInterval(this.timer)
}
render(){
return(
<h1>{this.state.date.toLocaleTimeString()}</h1>
)
}
?}
?export default Clock;
2018-10-09
hao