import React, { Component } from 'react';
import { Text, View } from 'react-native';
export default class Clock extends Component {
componentDidMount(){
setInterval(() => (
this.setState(
{ curHours : new Date().getHours()}
),
this.setState(
{ curMins : new Date().getMinutes()}
),
this.setState(
{ curSeconds : new Date().getSeconds()}
)
), 1000);
}
state = {curHours:new Date().getHours()};
state = {curMins:new Date().getMinutes()};
state = {curSeconds:new Date().getSeconds()};
renderHours() {
return (
<Text>{'Hours:'}{this.state.curHours}</Text>
);
}
renderMinutes() {
return (
<Text>{'Minutes:'}{this.state.curMinutes}</Text>
);
}
renderSeconds() {
return (
<Text>{'Seconds:'}{this.state.curSeconds}</Text>
);
}
}
- 我正在嘗試制作一個可以跟蹤時間的應用程式,有點像日常計劃。所以我需要在應用程式運行時實時獲取當前時間。例如,該應用程式應該告訴用戶他們未能在給定時間內完成某項任務。我嘗試匯出clock.js 并使用它的函式,但只有renderSeconds() 正在作業,其他的只顯示空白。
uj5u.com熱心網友回復:
當您定義state3 次時,只有最后一個被持久化,因為您覆寫了前一個變數。此外,您的初始狀態應該在建構式中宣告。將此添加到班級的頂部
constructor() {
this.state = {
curHours:new Date().getHours(),
curMins:new Date().getMinutes(),
curSeconds:new Date().getSeconds(),
}
}
uj5u.com熱心網友回復:
我認為功能組件解決這個問題會簡單得多,但這只是我的意見。這是一個示例的鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357624.html
標籤:javascript 反应 进口 出口
上一篇:RSpec:我們可以配置一個回傳兩個不同值的期望嗎?我想測驗重試機制
下一篇:通過嵌套屬性獲取父物件
