javascript - React Native Count One Up 函数
问题描述
我是 React / React Native 的新手,我正在尝试制作一个从 0 到 100 的函数并将其显示在图形仪表中。模拟从 0 到 100 MPH 的车速表。所以这是我的onPress函数,如果按下它,它将计数一个:
import React, { Component } from 'react';
import {
StyleSheet,
TouchableOpacity,
Text,
View,
} from 'react-native';
import Speedometer from 'react-native-speedometer-chart';
export default class App extends Component {
constructor(props) {
super(props)
this.state = { count: 0 }
}
onPress = () => {
this.setState({
count: this.state.count+1
})
}
然后,我添加了一个 while 语句,如果count小于 100,它将继续增加一个。问题是它不算数。
onPress = () => {
while(this.state.count < 100) {
this.setState({
count: this.state.count+1
})
}
}
有人可以帮忙吗。这是我拥有的完整代码:
import React, { Component } from 'react';
import {
StyleSheet,
TouchableOpacity,
Text,
View,
} from 'react-native';
import Speedometer from 'react-native-speedometer-chart';
export default class App extends Component {
constructor(props) {
super(props)
this.state = { count: 0 }
}
onPress = () => {
while(this.state.count <= 100) {
this.setState({
count: this.state.count+1
})
}
}
clear = () => {
this.setState({
count: this.state.count = 0
})
}
render() {
return (
<View style={styles.container}>
<Text style={styles.titleText}> AlphaDyne New Run Screen</Text>
<TouchableOpacity style={styles.button} onPress={this.onPress}>
<Text> Start Run </Text>
</TouchableOpacity>
<View style={[styles.countContainer]}>
<Speedometer
value={this.state.count !== 0 ? this.state.count: 0}
totalValue={100}
size={250}
outerColor="#d3d3d3"
internalColor="#ff0000"
showText
text="MPH"
textStyle={{ color: '#ff0000' }}
showLabels
labelStyle={{ color: '#FF00FF' }}
/>
<Text style={styles.countText}>{this.state.count !== 0 ? this.state.count: 0} MPH</Text>
<Text style={styles.space}></Text>
<Speedometer
value={this.state.count !== 0 ? this.state.count: 0}
totalValue={100}
size={250}
showIndicator
showLabels
labelStyle={{ color: '#FF00FF' }}
/>
<Text style={styles.countText}>{this.state.count !== 0 ? this.state.count: 0} MPH</Text>
<Text style={styles.spacer}></Text>
<Text style={styles.spacer}></Text>
<TouchableOpacity style={styles.button} onPress={this.clear}>
<Text> Clear </Text>
</TouchableOpacity>
</View>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 10
},
button: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10
},
countContainer: {
alignItems: 'center',
padding: 10,
marginTop: 20
},
countText: {
color: '#FF00FF',
fontSize: 20,
marginTop: 10
},
titleText: {
fontSize: 30,
alignSelf: 'center',
marginBottom: 20
},
space: {
marginTop: 20
}
});
谢谢!
解决方案
您必须调用功能 setState 来克服解决方案的异步问题:
onPress = () => {
while(this.state.count <= 100) {
this.setState(previousState => ({ ...previousState, count: previousState.count + 1 }));
}
}
推荐阅读
- r - 读取机器写入数据的 ZIP 文件不会在 RStudio 中“绘图”
- vue.js - $emit 完成时触发事件
- twitter-bootstrap - 什么是 ngx bootstrap、ng bootstrap、md bootrap 和 Angular Material Design?
- python - 将 Dict 写入 CSV 时出现 UnicodeEncodeError
- git - 输入密码时Gitlab控制台冻结
- reactjs - 访问 http://example.com 给出 502 Bad Gateway 但是 http://example.com:3000 给了我网站
- javascript - How do I reorder the dimensions of a rank 3 tensor in Tensorflow.js?
- json - 如何将子字符串值插入 PostgreSQL 中的 json 列?
- http - 422 Webchat ID 更改中无法处理的实体响应
- javascript - 如何找到文件读取的谷歌应用脚本限制