我正在尝试制作一个电话游戏,用户在其中猜测1到100之间的数字。如果按下按钮,则该语句应运行并进行一些更改。虽然什么都没有发生。为什么在按下按钮时不执行该语句?
const buttonPressed = () => {
if ({num} < {randomNum}) {
message = tooLow
} else if ({num} > {randomNum}) {
message = tooHigh
} else if ({num} === {randomNum}) {
message= correct
Alert.alert('You guessed the number in'+{counter}+'guesses')
}
counter += 1
}
return (
<View style={styles.container}>
<View>{message}</View>
<TextInput
style={styles.textinput}
keyboardType='numeric'
onChangeText={(num) => setNum(num)}
value={num}
/>
<Button onPress={buttonPressed} title="Press me"/>
</View>
);
}
答案 0 :(得分:0)
让我们做点更多的React方式。
您需要将此constructor(props) {
this.state = {
num: 0
}
}
const setNum = (num) => {
this.setState({num});
}
const buttonPressed = () => {
if ({this.props.num} < {randomNum}) {
message = tooLow
counter + 1
} else if ({this.props.num} > {randomNum}) {
message = tooHigh
counter + 1
} else if ({this.props.num} === {randomNum}) {
counter + 1
message= correct
Alert.alert('You guessed the number in'+{counter}+'guesses')
}
}
return (
<View style={styles.container}>
<View>{message}</View>
<TextInput
style={styles.textinput}
keyboardType='numeric'
onChangeText={(num) => this.setNum(num)}
value={this.props.num}
/>
<Button onPress={buttonPressed} title="Press me"/>
</View>
);
}
设为内部状态(如果您未使用Redux或Mobx或任何其他状态管理)
类似这样的东西:
using AecDb = Autodesk.Aec.DatabaseServices;
using AecPropDb = Autodesk.Aec.PropertyData.DatabaseServices;