react 显示当前时间_如何在React中动态显示时间日期

您需要使用该状态来保持当前周结束并开始。只要道具或状态发生变化,组件都会重新渲染。你需要保持这个数据的状态(如果使用redux,你会从道具中获取数据)。

下面是一个使用ES6一个例子(请确保您的WebPack配置包括巴贝尔与0级):

class WeekNav extends PureComponent {

componentWillMount() {

this.setState({

currentWeekStart: moment().startOf('week').format("MM/DD/YYYY"),

currentWeekEnd: moment().endOf('week').format("MM/DD/YYYY"),

});

}

prevWeek =() => {

const { currentWeekStart, currentWeekEnd } = this.state;

newStart = currentWeekStart.slice();

newEnd = currentWeekEnd.slice();

newWeekStart = moment(newStart).subtract(7, 'days').format("MM/DD/YYYY");

newWeekEnd = moment(newEnd).subtract(7, 'days').format("MM/DD/YYYY");

this.setState({

currentWeekStart: newWeekStart,

currentWeekEnd: newWeekEnd,

});

}

upcomingWeek =() => {

const { currentWeekStart, currentWeekEnd } = this.state;

newStart = currentWeekStart.slice();

newEnd = currentWeekEnd.slice();

newWeekStart = moment(newStart).add(7, 'days').format("MM/DD/YYYY");

newWeekEnd = moment(newEnd).add(7, 'days').format("MM/DD/YYYY");

this.setState({

currentWeekStart: newWeekStart,

currentWeekEnd: newWeekEnd,

});

}

render: function() {

const { currentWeekStart, currentWeekEnd } = this.state;

return (

{currentWeekStart} - {currentWeekEnd}

);

}

}

PS:这将会是很容易更新我以前的代码使用React.createClass到ES5。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值