如何在react.js组件中侦听DOM更新?

时间:2015-08-27 22:11:23

标签: reactjs

我试图弄清楚当我从中调用this.setState()时为什么react.js组件没有更新。我想尝试监听所有DOM更新作为调试方式。这可能吗?

var ChangingProgressBar = React.createClass({
  getInitialState: function(){
     return {'percentage': 0, 
            'mood':'progress-bar-info'}
  },
  componentWillMount: function(){
    self = this;
    setTimeout(function(){
      self.setState({'mood', 'progress-bar-danger'});
    }, 2000);
  },
  render: function(){
    return (
      <div className='progress-bar {this.state.mood}'
       role='progressbar' aria-valuenow='{this.state.percentage}'
       aria-valuemin='0' aria-valuemax='100'
       style="{{width: this.state.percentage + '%'}}">
         {this.state.percentage}%
      </div>
    )
  })
})

1 个答案:

答案 0 :(得分:0)

self.setState({'mood', 'progress-bar-danger'});

应该是

self.setState({'mood': 'progress-bar-danger'});