** aggiornato per implementare TimerMixin
È necessario chiamare un this.setState per aggiornare una variabile di stato, e come specificato dal @ eyal83, utilizzare il TimerMixin per 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);
}
Ho anche impostato un'app di base che reimposta la variabile di stato con un setInterval here, il codice è di sotto. 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);
fonte
2015-12-12 19:39:19
Si dovrebbe utilizzare il TimerMixin per setTimeout e setInterval. Vedi la mia risposta per ulteriori informazioni. – eyal83