gpt4 book ai didi

javascript - React Native Stack Navigator 不接受 Screen 作为组件 Prop 中的 Prop

转载 作者:行者123 更新时间:2023-12-04 07:19:21 26 4
gpt4 key购买 nike

我正在为这个 react 原生项目使用 expo v4.9.1
我正在按照这个 Document 实现堆栈导航器。它是这样的
App.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Provider } from 'react-redux'
import HomeScreen from './screens/HomeScreen';
import { store } from './store';
import { SafeAreaProvider } from 'react-native-safe-area-context'
import 'react-native-gesture-handler';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import MapScreen from './screens/MapScreen';
import 'react-native-gesture-handler';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
import { GOOGLE_MAPS_APIKEY } from '@env'

export default function App() {

const Stack = createStackNavigator();

return (
<Provider store={store}>
<NavigationContainer>
<SafeAreaProvider>
<Stack.Navigator>
<Stack.Screen name='HomeScreen' component={HomeScreen} options={{headerShown: false}}/>
<Stack.Screen name='MapScreen' component={MapScreen} options={{headerShown: false}}/>
<GooglePlacesAutocomplete
placeholder='Where From?'
nearbyPlacesAPI='GooglePlacesSearch'
debounce={300}
/>
</Stack.Navigator>
</SafeAreaProvider>
</NavigationContainer>
</Provider>

);
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
MapScreen 访问 HomeScreen HomeScreen.js
import React from 'react'
import { StyleSheet, Text, View, SafeAreaView, Image } from 'react-native'
import tw from 'tailwind-react-native-classnames'
import NavOptions from '../components/NavOptions'

const HomeScreen = () => {
return (
<SafeAreaView style={tw`bg-white h-full`}>
<View style={tw`p-5`}>
<Image
style={{
width: 100,
height:100,
resizeMode: 'contain',
}}
source={{
uri: 'https://links.papareact.com/gzs'
}}
/>

<NavOptions />
</View>
</SafeAreaView>
)
}

export default HomeScreen

const styles = StyleSheet.create({
text: {
color: 'blue',
}
})
NavOptions.js
import { useNavigation } from '@react-navigation/native';
import React from 'react'
import { FlatList, Image, Text, TouchableOpacity, View } from 'react-native'
import { Icon } from 'react-native-elements/dist/icons/Icon';
import tw from 'tailwind-react-native-classnames'

const data =[
{
id: '123',
title: 'Get a ride',
image: 'https://links.papareact.com/3pn',
screen: 'MapScreen',
},
{
id: '456',
title: 'Order food',
image: 'https://links.papareact.com/28w',
screen: 'EatScreen',
}
];

const NavOptions = () => {

const navigation = useNavigation();

return (
<FlatList
data={data}
horizontal
keyExtractor={(item) => item.id}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => navigation.navigate(item.screen)}
style={tw`p-2 pl-6 pb-8 pt-4 bg-gray-200 m-2 w-40 rounded-xl`}
>
<View>
<Image
style={{ width: 120, height: 120, resizeMode: 'contain' }}
source={{uri: item.image}}
/>
<Text style={tw`mt-2 text-lg font-semibold`}>{item.title}</Text>
<Icon
style={tw`p-2 bg-black rounded-full w-10 mt-4`}
type='antdesign'
name='arrowright'
color='white'
/>
</View>
</TouchableOpacity>

)}
/>
)
}

export default NavOptions
我使用了 useNavigation() 钩子(Hook)。然而我得到了错误
A navigator can only contain 'Screen' components as its direct children (found 'undefined'). To render this component in the navigator, pass it in the 'component' prop to 'Screen'.

This error is located at:
in StackNavigator (at App.js:23)
in RNCSafeAreaProvider (at SafeAreaContext.tsx:76)
in SafeAreaProvider (at App.js:22)
in EnsureSingleNavigator (at BaseNavigationContainer.tsx:409)
in ForwardRef(BaseNavigationContainer) (at NavigationContainer.tsx:91)
in ThemeProvider (at NavigationContainer.tsx:90)
in ForwardRef(NavigationContainer) (at App.js:21)
in Provider (at App.js:20)
in App (created by ExpoRoot)
in ExpoRoot (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)
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:171:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue
StackOverflow threadGitHub issues thread 中的完全相同的问题提供了一些解决方案。它们都不适合我,而且似乎没有关于这个错误是如何发生的确切解释。我也在使用 节点 v14.16.0 npm v7.20.1

最佳答案

所以我深入研究了react-navigation的源代码并为您找到答案。根据https://github.com/react-navigation/react-navigation/blob/42a875212c5071b12e92eaf159ef488365413ab8/packages/core/src/useNavigationBuilder.tsx <Stack.Navigator>被设计为只有 Screen要素:

if (React.isValidElement(child)) {
if (child.type === Screen) {
// We can only extract the config from `Screen` elements
// If something else was rendered, it's probably a bug
acc.push([...
这就是为什么放置 <GooglePlacesAutocomplete>里面 <Stack.Navigator>你有错误
throw new Error(
`A navigator can only contain 'Screen' as its direct children ...
);

关于javascript - React Native Stack Navigator 不接受 Screen 作为组件 Prop 中的 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68599701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com