我是 RN 開發(fā)的新手,面臨以下問題:當我用 Expo 創(chuàng)建 RN 項目時,我從屏幕上刪除了所有內容并添加了必要的組件。所以,我有一個TextInput,它應該將數(shù)據(jù)添加到變量中。所以,這就是我在 state: 中創(chuàng)建屬性的原因 this.state = {text: ''};,它將保留這些數(shù)據(jù)。但是當我運行項目時,我有這個錯誤:undefined is not an object. 這是我的屏幕代碼:export default function HomeScreen() { this.state = {text: ''}; return ( <View style={styles.container}> <ScrollView style={styles.scrollViewContainer} contentContainerStyle={styles.contentContainer}> <View style={styles.searchContainer}> <TextInput placeHolder="Type something!" onChangeText={(text) => this.setState({text})} value={this.state.text} /> </View> <View> <Button onPress={Alert.alert("Button pressed!")} title="Search" /> </View> <View style={styles.listContainer}> <Text>{this.state.text}</Text> </View> </ScrollView> </View> ) ;}所以,也許這個問題很愚蠢,但請解釋這是什么問題以及我該如何解決這個問題。也許代碼中還有其他一些時刻,哪些應該改變?
在 Expo 項目中從 TextInput 獲取文本時 undefined 不是對象
達令說
2021-08-26 19:28:04