Chr*_*art 7 react-native react-native-android
我有以下js:
import React, { useState, useEffect, useRef } from 'react';
import {
Text,
View,
TextInput,
Alert,
Image,
TouchableOpacity,
} from 'react-native';
import 'react-native-get-random-values';
import Button from 'react-native-button';
import PhoneInput from 'react-native-phone-input';
import { FirebaseRecaptchaVerifierModal } from 'expo-firebase-recaptcha';
import CodeField from 'react-native-confirmation-code-field';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import { useColorScheme } from 'react-native-appearance';
import TNActivityIndicator from '../../truly-native/TNActivityIndicator';
import TNProfilePictureSelector from '../../truly-native/TNProfilePictureSelector/TNProfilePictureSelector';
import CountriesModalPicker from '../../truly-native/CountriesModalPicker/CountriesModalPicker';
import { IMLocalized } from '../../localization/IMLocalization';
import { setUserData } from '../redux/auth';
import { connect } from 'react-redux';
import authManager from '../utils/authManager';
import { localizedErrorMessage } from '../utils/ErrorCode';
import TermsOfUseView from '../components/TermsOfUseView';
import { firebase } from '../../firebase/config';
import dynamicStyles from './styles';
const SmsAuthenticationScreen = (props) => {
const appConfig =
props.navigation.state.params.appConfig ||
props.navigation.getParam('appConfig');
const appStyles =
props.navigation.state.params.appStyles ||
props.navigation.getParam('appStyles');
const colorScheme = useColorScheme();
const styles = dynamicStyles(appStyles, colorScheme);
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [loading, setLoading] = useState(false);
const [isPhoneVisible, setIsPhoneVisible] = useState(true);
const [phoneNumber, setPhoneNumber] = useState(false);
const [countriesPickerData, setCountriesPickerData] = useState(null);
const [verificationId, setVerificationId] = useState(null);
const [profilePictureURL, setProfilePictureURL] = useState(null);
const [countryModalVisible, setCountryModalVisible] = useState(false);
const myCodeInput = useRef(null);
const phoneRef = useRef(null);
const recaptchaVerifier = React.useRef(null);
const firebaseConfig = firebase.app().options;
const { isSigningUp } = props.navigation.state.params;
useEffect(() => {
if (phoneRef && phoneRef.current) {
setCountriesPickerData(phoneRef.current.getPickerData());
}
}, [phoneRef]);
const onFBButtonPress = () => {
authManager
.loginOrSignUpWithFacebook(appConfig.appIdentifier)
.then((response) => {
if (response.user) {
const user = response.user;
props.setUserData({ user });
props.navigation.navigate('MainStack', { user: user });
} else {
Alert.alert(
'',
localizedErrorMessage(response.error),
[{ text: IMLocalized('OK') }],
{
cancelable: false,
},
);
}
});
};
const signInWithPhoneNumber = (userValidPhoneNumber) => {
setLoading(true);
authManager.onVerification(userValidPhoneNumber);
authManager
.sendSMSToPhoneNumber(userValidPhoneNumber, recaptchaVerifier.current)
.then((response) => {
const confirmationResult = response.confirmationResult;
if (confirmationResult) {
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
setVerificationId(confirmationResult.verificationId);
setIsPhoneVisible(false);
setLoading(false);
} else {
// Error; SMS not sent
setLoading(false);
Alert.alert(
'',
localizedErrorMessage(response.error),
[{ text: IMLocalized('OK') }],
{ cancelable: false },
);
}
});
};
const signUpWithPhoneNumber = (smsCode) => {
const userDetails = {
firstName,
lastName,
phone: phoneNumber,
photoURI: profilePictureURL,
};
setLoading(true);
authManager
.registerWithPhoneNumber(
userDetails,
smsCode,
verificationId,
appConfig.appIdentifier,
)
.then((response) => {
if (response.error) {
Alert.alert(
'',
localizedErrorMessage(response.error),
[{ text: IMLocalized('OK') }],
{ cancelable: false },
);
} else {
const user = response.user;
props.setUserData({ user });
props.navigation.navigate('MainStack', { user: user });
}
setLoading(false);
});
};
const onPressSend = () => {
if (phoneRef.current.isValidNumber()) {
const userValidPhoneNumber = phoneRef.current.getValue();
setLoading(true);
setPhoneNumber(userValidPhoneNumber);
if (!isSigningUp) {
// If this is a login attempt, we first need to check that the user associated to this phone number exists
authManager
.retrieveUserByPhone(userValidPhoneNumber)
.then((response) => {
if (response.success) {
signInWithPhoneNumber(userValidPhoneNumber);
} else {
setPhoneNumber(null);
setLoading(false);
Alert.alert(
'',
IMLocalized(
'You cannot log in. There is no account with this phone number.',
),
[{ text: IMLocalized('OK') }],
{
cancelable: false,
},
);
}
});
} else {
signInWithPhoneNumber(userValidPhoneNumber);
}
} else {
Alert.alert(
'',
IMLocalized('Please enter a valid phone number.'),
[{ text: IMLocalized('OK') }],
{
cancelable: false,
},
);
}
};
const onPressFlag = () => {
setCountryModalVisible(true);
};
const onPressCancelContryModalPicker = () => {
setCountryModalVisible(false);
};
const onFinishCheckingCode = (newCode) => {
setLoading(true);
if (isSigningUp) {
signUpWithPhoneNumber(newCode);
} else {
authManager.loginWithSMSCode(newCode, verificationId).then((response) => {
if (response.error) {
Alert.alert(
'',
localizedErrorMessage(response.error),
[{ text: IMLocalized('OK') }],
{ cancelable: false },
);
} else {
const user = response.user;
props.setUserData({ user });
props.navigation.navigate('MainStack', { user: user });
}
setLoading(false);
});
}
};
const phoneInputRender = () => {
return (
<>
<PhoneInput
style={styles.InputContainer}
flagStyle={styles.flagStyle}
textStyle={styles.phoneInputTextStyle}
ref={phoneRef}
onPressFlag={onPressFlag}
offset={10}
allowZeroAfterCountryCode
textProps={{
placeholder: IMLocalized('Phone number'),
placeholderTextColor: '#aaaaaa',
}}
/>
{countriesPickerData && (
<CountriesModalPicker
data={countriesPickerData}
appStyles={appStyles}
onChange={(country) => {
selectCountry(country);
}}
cancelText={IMLocalized('Cancel')}
visible={countryModalVisible}
onCancel={onPressCancelContryModalPicker}
/>
)}
<Button
containerStyle={styles.sendContainer}
style={styles.sendText}
onPress={() => onPressSend()}>
{IMLocalized('Send code')}
</Button>
</>
);
};
const codeInputRender = () => {
return (
<>
<CodeField
ref={myCodeInput}
inputPosition="full-width"
variant="border-b"
codeLength={6}
size={50}
space={8}
keyboardType="numeric"
cellProps={{ style: styles.input }}
containerProps={{ style: styles.codeFieldContainer }}
onFulfill={onFinishCheckingCode}
/>
</>
);
};
const selectCountry = (country) => {
phoneRef.current.selectCountry(country.iso2);
};
const renderAsSignUpState = () => {
return (
<>
<Text style={styles.title}>{IMLocalized('Create new account')}</Text>
<TNProfilePictureSelector
setProfilePictureURL={setProfilePictureURL}
appStyles={appStyles}
/>
<TextInput
style={styles.InputContainer}
placeholder={IMLocalized('First Name')}
placeholderTextColor="#aaaaaa"
onChangeText={(text) => setFirstName(text)}
value={firstName}
underlineColorAndroid="transparent"
/>
<TextInput
style={styles.InputContainer}
placeholder={IMLocalized('Last Name')}
placeholderTextColor="#aaaaaa"
onChangeText={(text) => setLastName(text)}
value={lastName}
underlineColorAndroid="transparent"
/>
{isPhoneVisible ? phoneInputRender() : codeInputRender()}
<Text style={styles.orTextStyle}> {IMLocalized('OR')}</Text>
<Button
containerStyle={styles.signWithEmailContainer}
onPress={() =>
props.navigation.navigate('Signup', { appStyles, appConfig })
}>
{IMLocalized('Sign up with E-mail')}
</Button>
</>
);
};
const renderAsLoginState = () => {
return (
<>
<Text style={styles.title}>{IMLocalized('Sign In')}</Text>
{isPhoneVisible ? phoneInputRender() : codeInputRender()}
<Text style={styles.orTextStyle}> {IMLocalized('OR')}</Text>
<Button
containerStyle={styles.facebookContainer}
style={styles.facebookText}
onPress={() => onFBButtonPress()}>
{IMLocalized('Login With Facebook')}
</Button>
<Button
containerStyle={styles.signWithEmailContainer}
onPress={() =>
props.navigation.navigate('Login', { appStyles, appConfig })
}>
{IMLocalized('Sign in with E-mail')}
</Button>
</>
);
};
return (
<View style={styles.container}>
<KeyboardAwareScrollView
style={{ flex: 1, width: '100%' }}
keyboardShouldPersistTaps="always">
<TouchableOpacity onPress={() => props.navigation.goBack()}>
<Image
style={appStyles.styleSet.backArrowStyle}
source={appStyles.iconSet.backArrow}
/>
</TouchableOpacity>
{isSigningUp ? renderAsSignUpState() : renderAsLoginState()}
{isSigningUp && (
<TermsOfUseView tosLink={appConfig.tosLink} style={styles.tos} />
)}
<FirebaseRecaptchaVerifierModal
ref={recaptchaVerifier}
firebaseConfig={firebaseConfig}
/>
</KeyboardAwareScrollView>
{loading && <TNActivityIndicator appStyles={appStyles} />}
</View>
);
};
export default connect(null, {
setUserData,
})(SmsAuthenticationScreen);
Run Code Online (Sandbox Code Playgroud)
升级到最新版本的 React Native 后,我收到以下错误和堆栈:
错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。
检查 的渲染方法SmsAuthenticationScreen。
This error is located at:
in RCTView (at View.js:34)
in View (at ScrollView.js:1124)
in RCTScrollView (at ScrollView.js:1260)
in ScrollView (at ScrollView.js:1286)
in ScrollView (at KeyboardAwareHOC.js:517)
in KeyboardAwareScrollView (at SmsAuthenticationScreen.js:340)
in RCTView (at View.js:34)
in View (at SmsAuthenticationScreen.js:339)
in SmsAuthenticationScreen (created by ConnectFunction)
in ConnectFunction (created by SceneView)
in SceneView (created by CardContainer)
in RCTView (at View.js:34)
in View (created by CardContainer)
in RCTView (at View.js:34)
in View (created by CardContainer)
in RCTView (at View.js:34)
in View (created by ForwardRef(CardSheet))
in ForwardRef(CardSheet) (created by Card)
in RCTView (at View.js:34)
in View (at createAnimatedComponent.js:165)
in AnimatedComponent (at createAnimatedComponent.js:215)
in ForwardRef(AnimatedComponentWrapper) (created by PanGestureHandler)
in PanGestureHandler (created by PanGestureHandler)
in PanGestureHandler (created by Card)
in RCTView (at View.js:34)
in View (at createAnimatedComponent.js:165)
in AnimatedComponent (at createAnimatedComponent.js:215)
in ForwardRef(AnimatedComponentWrapper) (created by Card)
in RCTView (at View.js:34)
in View (created by Card)
in Card (created by CardContainer)
in CardContainer (created by Context.Consumer)
in RNSScreen (at createAnimatedComponent.js:165)
in AnimatedComponent (at createAnimatedComponent.js:215)
in ForwardRef(AnimatedComponentWrapper) (at src/index.native.js:111)
in Screen (created by MaybeScreen)
in MaybeScreen (created by Context.Consumer)
in RNSScreenContainer (at src/index.native.js:136)
in ScreenContainer (created by MaybeScreenContainer)
in MaybeScreenContainer (created by Context.Consumer)
in CardStack (created by KeyboardManager)
in KeyboardManager (created by Context.Consumer)
in RNCSafeAreaProvider (at SafeAreaContext.tsx:74)
in SafeAreaProvider (created by Context.Consumer)
in SafeAreaProviderCompat (created by StackView)
in GestureHandlerRootView (at GestureHandlerRootView.android.js:31)
in GestureHandlerRootView (created by StackView)
in StackView (created by StackView)
in StackView
in Unknown (created by Navigator)
in Navigator (created by SceneView)
in SceneView (created by SwitchView)
in SwitchView (created by Navigator)
in Navigator (at create-redux-container.js:93)
in NavigatorReduxWrapper (created by ConnectFunction)
in ConnectFunction (at App.js:48)
in RCTView (at View.js:34)
in View (created by MenuProvider)
in RCTView (at View.js:34)
in View (created by MenuProvider)
in MenuProvider (at App.js:47)
in RCTView (at View.js:34)
in View (at NativeAppearance.tsx:4)
in FallbackAppearanceProvider (at src/index.tsx:70)
in AppearanceProvider (at App.js:46)
in Provider (at App.js:45)
in App (at renderApplication.js:45)
in RCTView (at View.js:34)
in View (at AppContainer.js:106)
in RCTView (at View.js:34)
in View (at AppContainer.js:132)
in AppContainer (at renderApplication.js:39)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了其他文章和 stackoverflow 请求提到的东西,但到目前为止没有运气。React Native 有点新,所以可能会遗漏一些简单的东西。
错误的另一种排列:
ERROR Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or
you might have mixed up default and named imports.
Check the render method of `SmsAuthenticationScreen`.
This error is located at:
in RCTView (at View.js:34)
in View (at SmsAuthenticationScreen.js:339)
in SmsAuthenticationScreen (created by ConnectFunction)
in ConnectFunction (created by SceneView)
in SceneView (created by CardContainer)
in RCTView (at View.js:34)
in View (created by CardContainer)
in RCTView (at View.js:34)
in View (created by CardContainer)
in RCTView (at View.js:34)
in View (created by ForwardRef(CardSheet))
in ForwardRef(CardSheet) (created by Card)
in RCTView (at View.js:34)
in View (at createAnimatedComponent.js:165)
in AnimatedComponent (at createAnimatedComponent.js:215)
in ForwardRef(AnimatedComponentWrapper) (created by PanGestureHandler)
in PanGestureHandler (created by PanGestureHandler)
in PanGestureHandler (created by Card)
in RCTView (at View.js:34)
in View (at createAnimatedComponent.js:165)
in AnimatedComponent (at createAnimatedComponent.js:215)
in ForwardRef(AnimatedComponentWrapper) (created by Card)
in RCTView (at View.js:34)
in View (created by Card)
in Card (created by CardContainer)
in CardContainer (created by Context.Consumer)
in RNSScreen (at createAnimatedComponent.js:165)
in AnimatedComponent (at createAnimatedComponent.js:215)
in ForwardRef(AnimatedComponentWrapper) (at src/index.native.js:111)
in Screen (created by MaybeScreen)
in MaybeScreen (created by Context.Consumer)
in RNSScreenContainer (at src/index.native.js:136)
in ScreenContainer (created by MaybeScreenContainer)
in MaybeScreenContainer (created by Context.Consumer)
in CardStack (created by KeyboardManager)
in KeyboardManager (created by Context.Consumer)
in RNCSafeAreaProvider (at SafeAreaContext.tsx:74)
in SafeAreaProvider (created by Context.Consumer)
in SafeAreaProviderCompat (created by StackView)
in GestureHandlerRootView (at GestureHandlerRootView.android.js:31)
in GestureHandlerRootView (created by StackView)
in StackView (created by StackView)
in StackView
in Unknown (created by Navigator)
in Navigator (created by SceneView)
in SceneView (created by SwitchView)
in SwitchView (created by Navigator)
in Navigator (at create-redux-container.js:93)
in NavigatorReduxWrapper (created by ConnectFunction)
in ConnectFunction (at App.js:48)
in RCTView (at View.js:34)
in View (created by MenuProvider)
in RCTView (at View.js:34)
in View (created by MenuProvider)
in MenuProvider (at App.js:47)
in RCTView (at View.js:34)
in View (at NativeAppearance.tsx:4)
in FallbackAppearanceProvider (at src/index.tsx:70)
in AppearanceProvider (at App.js:46)
in Provider (at App.js:45)
in App (at renderApplication.js:45)
in RCTView (at View.js:34)
in View (at AppContainer.js:106)
in RCTView (at View.js:34)
in View (at AppContainer.js:132)
in AppContainer (at renderApplication.js:39)
Run Code Online (Sandbox Code Playgroud)
这是 package.json
{
"name": "MyApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"prettier": "prettier --write '*.js' 'src/**/*.js'",
"test": "jest",
"lint": "eslint .",
"android": " cd android && ./gradlew clean && cd .. && react-native run-android",
"ios": "react-native run-ios"
},
"dependencies": {
"@react-native-community/art": "^1.2.0",
"@react-native-community/async-storage": "^1.12.0",
"@react-native-community/cameraroll": "^4.0.0",
"@react-native-community/geolocation": "^2.0.2",
"@react-native-community/masked-view": "^0.1.10",
"@react-native-firebase/app": "^6.7.1",
"@react-native-firebase/auth": "^6.7.1",
"@react-native-firebase/messaging": "^6.7.1",
"@skele/components": "^1.0.0-alpha.40",
"axios": "^0.19.0",
"base-64": "^0.1.0",
"crypto-js": "^3.1.9-1",
"expo-av": "~8.0.0",
"expo-blur": "^8.1.0",
"expo-camera": "^8.0.0",
"expo-constants": "~8.0.0",
"expo-facebook": "~8.0.0",
"expo-file-system": "^8.1.0",
"expo-firebase-recaptcha": "^1.1.3",
"expo-image-picker": "~8.0.1",
"expo-localization": "~8.0
从您的日志来看,该错误似乎是由于KeyboardAwareScrollView模块而出现的。在快速搜索该模块的 github 存储库后,我发现了以下内容: https: //github.com/APSL/react-native-keyboard-aware-scroll-view/issues/443
该问题仍未解决,但您可以尝试上面的链接中提供的解决方法。我只需卸载react-native-keyboard-aware-scroll-view并安装这个叉子即可解决此问题:
npm i @codler/react-native-keyboard-aware-scroll-view
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1181 次 |
| 最近记录: |