React Native渲染报错:Property 'TextInput'不存在问题求助
解决「Property 'TextInput' doesn't exist」渲染错误
出现这个错误的核心原因:你在代码中使用了TextInput和TouchableOpacity组件,但没有从react-native库中显式导入它们。React Native的所有内置UI组件都需要手动导入才能使用。
修复步骤:
- 修改LoginScreen.js的导入语句,添加缺失的组件:
将原导入行:
import {View, Text, StyleSheet} from 'react-native';
替换为:
import {View, Text, StyleSheet, TextInput, TouchableOpacity} from 'react-native';
- 对RegisterScreen.js执行同样操作——因为它的结构和LoginScreen.js类似,必然也存在相同的组件未导入问题。
修改后的完整LoginScreen.js代码:
import React from 'react'; import {View, Text, StyleSheet, TextInput, TouchableOpacity} from 'react-native'; import { signInWithEmailAndPassword } from 'firebase/auth'; import {initializeApp} from 'firebase/app'; import {getAnalytics} from 'firebase/analytics'; import { getAuth } from 'firebase/auth'; // Your web app's Firebase configuration // For Firebase JS SDK v7.20.0 and later, measurementId is optional const firebaseConfig = { [hidden] }; // Initialize Firebase const app = initializeApp(firebaseConfig); const auth = getAuth(app); export default class LoginScreen extends React.Component { state = { email: "", password: "", errorMessage: null } handleLogin = () => { const {email, password} = this.state signInWithEmailAndPassword(auth, email, password) .catch(error => this.setState({errorMessage: error.message})) } render() { return ( <View style={styles.container}> <Text style={styles.greeting}> {'Hello again. \nWelcome back.'} </Text> <View style={styles.errorMessage}> {this.state.errorMessage && <Text style={styles.error}>{this.state.errorMessage}</Text>} </View> <View style={styles.form}> <View> <TextInput style={styles.input} autoCapitalize="none" placeholder="Email address" onChangeText={email => this.setState({email})} value={this.state.email} /> </View> <View style={{marginTop: 32}}> <TextInput style={styles.input} secureTextEntry autoCapitalize="none" placeholder="Password" onChangeText={password => this.setState({password})} value={this.state.password} /> </View> </View> <TouchableOpacity style={styles.button} onPress={this.handleLogin}> <Text style={{ color: "#FFF", fontWeight: "500"}}>Log in</Text> </TouchableOpacity> </View> ); } }
内容的提问来源于stack exchange,提问作者proshanto-c
相关产品推荐
相关产品推荐

