window.requestAnimationFrame() 主要用于浏览器上的动画优化。你可以在这里阅读更多信息:http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
但我认为它不能帮助您解决问题。据我了解,您的问题出在您的反应组件上。
以下是您的架构可能实现的示例:https://jsfiddle.net/snahedis/69z2wepo/28572/
var Master = React.createClass({
increment: function() {
this.refs.counter.increment();
},
render: function() {
return (
<div>
<Counter ref="counter" />
<Logic increment={this.increment} />
</div>
);
}
});
var Logic = React.createClass({
render: function() {
return <button onClick={this.props.increment}>increment</button>;
}
});
var Counter = React.createClass({
getInitialState: function() {
return {
counter: 0
};
},
increment: function() {
this.setState({
counter: this.state.counter + 1
});
},
render: function() {
return <div>{this.state.counter}</div>;
}
});
ReactDOM.render(
<Master />,
document.getElementById('container')
);
显然,逻辑组件上的增量方法可以通过单击按钮来触发。
不过,这个结构有点奇怪。如果可能的话,我会建议改变它。 Logic 组件将成为 Counter 组件的父组件,而不是其兄弟组件。
示例如下:https://jsfiddle.net/snahedis/69z2wepo/28573/
var Master = React.createClass({
render: function() {
return (
<div>
<CounterLogicWrapper />
</div>
);
}
});
var CounterLogicWrapper = React.createClass({
getInitialState: function() {
return {
counter: 0
};
},
increment: function() {
this.setState({
counter: this.state.counter + 1
});
},
render: function() {
return (
<div>
<Counter counter={this.state.counter} />
<button onClick={this.increment}>increment</button>
</div>
);
}
});
var Counter = React.createClass({
render: function() {
return <div>{this.props.counter}</div>;
}
});
ReactDOM.render(
<Master />,
document.getElementById('container')
);