【发布时间】:2021-02-11 14:10:46
【问题描述】:
我正在学习 React Native,我正在创建一个简单的应用程序来执行二次公式的解决方案。
问题出在我使用“onChangeText”道具在 TextInput 中捕获数字的那一刻。 react-native 文档说“onChangeText”道具返回一个简单的字符串值。话虽如此,我尝试使用 parseInt () 和 Number () 函数将“onChangeText”的值转换为 int,但没有任何效果,结果始终为 NaN。
有人知道如何将 TextInput 的值转换为 int 吗?
const App = () => {
var _a;
var _b;
var _c;
var res1;
var res2;
const result = () => {
Number(_a);
Number(_b);
Number(_c);
res1 = -1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c)) / (2 * a);
res2 = -1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c)) / (2 * a);
}
return (
<>
<ImageBackground source={image} style={styles.img}>
<ScrollView>
<View style={styles.title}>
<Text style={styles.title_text}>
Formula General
</Text>
</View>
<View style={{marginTop: 10}}>
<View style={styles.container}>
<Text style={styles.text}>
Introduce el valor de A:
</Text>
<TextInput style={styles.textinput} placeholder='ej. 10' onChangeText={(val) => a_=val}></TextInput>
</View>
<View style={styles.container}>
<Text style={styles.text}>
Introduce el valor de B:
</Text>
<TextInput style={styles.textinput} placeholder='ej. 3' onChangeText={(val) => b_ = val}></TextInput>
</View>
<View style={styles.container}>
<Text style={styles.text}>
Introduce el valor de C:
</Text>
<TextInput style={styles.textinput} placeholder='ej. 3' onChangeText={(val) => c_set=val}></TextInput>
<TouchableOpacity style={{backgroundColor:'#07057E', marginTop: 95, width: 100, marginLeft:0, position: 'absolute', paddingLeft: 20, paddingBottom:20, paddingRight: 20, paddingTop: 10}} onPress={operacion}>
<Text style={{color:'#fff', fontWeight:'bold', fontSize:11}}>Hacer Operacion</Text>
</TouchableOpacity>
</View>
</View>
<View style={{alignItems: 'flex-start', flexDirection:'column', justifyContent:'flex-start', position: 'absolute'}}>
<View style={styles.result}>
<Text style={styles.text}>
Resultado:
</Text>
<Text style={styles.res}>
Solucion 1: {res1}{'\n'}{'\n'}{'\n'}{'\n'}{'\n'}
Solucion 2: {res2}
</Text>
</View>
</View>
</ScrollView>
</ImageBackground>
</>
);
};
【问题讨论】:
标签: react-native type-conversion