** Cập nhật để thực hiện TimerMixin
Bạn cần phải gọi một this.setState để cập nhật một biến trạng thái, và theo quy định của @ eyal83, sử dụng TimerMixin cho setTimeout & setInterval:
var TimerMixin = require('react-timer-mixin');
componentDidMount: function() {
this.setInterval(() => {
let d = new Date();
let result = d.getHours() + d.getMinutes()/MINUTES_IN_HOUR;
this.setState({
timeLineTop: result
})
}, 500);
}
Tôi cũng đã thiết lập một ứng dụng cơ bản đặt lại biến trạng thái với mã setInterval here, dưới đây. https://rnplay.org/apps/9gD-Nw
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var TimerMixin = require('react-timer-mixin');
var SampleApp = React.createClass({
mixins: [TimerMixin],
getInitialState: function() {
return {
timeLineTop: 75
}
},
componentDidMount: function() {
this.setInterval(() => {
this.setState({
timeLineTop: this.state.timeLineTop+1
})
}, 500);
},
render: function() {
return (
<View style={styles.container}>
<View style={[
{ marginTop: this.state.timeLineTop },
]}><Text>TOP - {this.state.timeLineTop}</Text></View>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
marginTop:60,
},
});
AppRegistry.registerComponent('SampleApp',() => SampleApp);
Nguồn
2015-12-12 19:39:19
Bạn thực sự nên sử dụng TimerMixin cho setTimeout và setInterval. Xem câu trả lời của tôi để biết thêm thông tin. – eyal83