所以我有這個作業來檢查分數是否可以通過考試,下面是我的代碼:
import React, {useState} from 'react';
import {
Text,
View,
Image,
TextInput,
Button,
StyleSheet,
Alert,
} from 'react-native';
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
});
class ComponentAction {
static onPressButton() {
if (setScore < 50) {
Alert.alert('Sorry! You failed the test');
} else {
Alert.alert('Congrats! You passed the test');
}
}
}
const AppTextInput = () => {
const [score, setScore] = useState('');
return (
<View>
<Text>Score</Text>
<TextInput
placeholder="Input your score here"
keyboardType="numeric"
value={score}
style={styles.input}
onChangeText={score => setScore(score)}
/>
<Button title="Submit" onPress={() => ComponentAction.onPressButton()} />
</View>
);
};
export default AppTextInput;
它一直說他們找不到變數。我對 React Native 的東西有點陌生,如果你們能幫助我,我真的很感激
uj5u.com熱心網友回復:
你以錯誤的方式實施了它。處理程式函式應該在你的AppTextInput. 如果你想從另一個組件傳遞它,那就是另一回事了。但是對于當前的實作:
const AppTextInput = () => {
const [score, setScore] = useState('');
const onPressButton = () => {
if (setScore < 50) {
Alert.alert('Sorry! You failed the test');
} else {
Alert.alert('Congrats! You passed the test');
}
}
return (
<View>
<Text>Score</Text>
<TextInput
placeholder="Input your score here"
keyboardType="numeric"
value={score}
style={styles.input}
onChangeText={score => setScore(score)}
/>
<Button title="Submit" onPress={onPressButton} />
</View>
);
};
只需要onPressButton在AppTextInput組件內部定義處理程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/342439.html
