如何知道react对象的值_reactjs怎么实现监听数据对象

之前一直在用vuejs,因为最近开发要用到reactjs

但是发现他没有vuejs这种可以watch数据对象的方法,

请问下有过reactjs开发经验的人,如果我想监听一个state里的某个属性变化要怎么做?

我现在有个例子因为是定时器的关系,在里面用了setState然后执行后会一直报

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Main component.

我想把他拿出来,但是如果没有监听某个值又不知道怎么放出来

class Main extends React.Component {

constructor() {

super();

this.isCancelTimer = false;

this.state = {

isTimeOut: false,

isSubmit: true

};

}

setTimer(severTime) {

let currentDate = 0,

timer = null,

newSeverTime = '',

counter =()=>{

if (currentDate <= 0 || this.isCancelTimer) {

clearTimeout(timer);

//这里的setState怎么拿到外面

this.setState(Object.assign(this.state, {

isTimeOut: true,

isSubmit: false

}))

}

currentDate--;

timer = setTimeout(() => {

let day = Math.floor(currentDate / 60 / 60 / 24),

hours = Math.floor(currentDate / 60 / 60 % 24),

minutes = Math.floor(currentDate / 60 % 60),

seconds = currentDate % 60;

if (!this.isCancelTimer) {

this.setState(Object.assign({}, this.state, {

timer: (''+day).padStart(2, 0)+'天'+(''+hours).padStart(2, 0)+'时'+(''+minutes).padStart(2, 0)+'分'+(''+seconds).padStart(2, 0)+'秒'

}));

}

counter();

}, 1000);

};

// 适配IOS

newSeverTime = severTime.replace(/-/g, (value) => '/');

currentDate = +((+new Date(newSeverTime) - (+new Date())) / 1000).toFixed(0);

currentDate = currentDate.toFixed(0);

counter();

}

componentDidMount() {

this.setTimer('2017-11-18 17:10:50')

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值