export default function FoodScreen({ style }) { Cannot read property 'style' of…
- User
export default function FoodScreen({ style }) { Cannot read property 'style' of undefined import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { NavigationContainer } from '@react-navigation/native';
// Screens import FoodScreen from './screens/FoodScreen'; import MapScreen from './screens/MapScreen';
import Ionicons from '@expo/vector-icons/Ionicons';
const Tab = createBottomTabNavigator();
function Tabs({ style }) { return ( <Tab.Navigator screenOptions={{ tabBarStyle: { shadowOpacity: 0.3, shadowRadius: 10, } }}> <Tab.Screen name="FoodScreen" component={FoodScreen(style)} options={{ title: 'Food', headerShown: false, tabBarIcon: ({ focused }) => ( <Ionicons name={focused ? 'fast-food' : 'fast-food-outline'} size={24} color="black" /> ) }} /> <Tab.Screen name="MapScreen" component={MapScreen(style)} options={{ title: 'Map', headerShown: false, tabBarIcon: ({ focused }) => ( <Ionicons name={focused ? 'map' : 'map-outline'} size={24} color="black" /> ) }} /> </Tab.Navigator> ); }
const Stack = createNativeStackNavigator();
function Navigation({ style }) { return ( <Stack.Navigator> <Stack.Screen name="Main" component={Tabs(style)} options={{ headerShown: false }} /> </Stack.Navigator> ) }
export default Navigation
- Assistant
The code you provided has a couple of issues that are causing the error messages and improper functionality. Here's a corrected version along with explanations:
-
The
componentprop ofTab.Screenexpects a React component, but you are calling it as a function (FoodScreen(style)). This is causing the error. Instead, you should pass the component itself and handle thestyleprop inside the component. -
Since you're passing the
styleprop to your screens, you need to ensure it gets handled appropriately.
Below is the fixed code:
import React from 'react'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { NavigationContainer } from '@react-navigation/native'; import Ionicons from '@expo/vector-icons/Ionicons'; // Screens import FoodScreen from './screens/FoodScreen'; import MapScreen from './screens/MapScreen'; const Tab = createBottomTabNavigator(); function Tabs({ style }) { return ( <Tab.Navigator screenOptions={{ tabBarStyle: { shadowOpacity: 0.3, shadowRadius: 10, } }} > <Tab.Screen name="FoodScreen" options={{ title: 'Food', headerShown: false, tabBarIcon: ({ focused }) => ( <Ionicons name={focused ? 'fast-food' : 'fast-food-outline'} size={24} color="black" /> ) }} > {props => <FoodScreen {...props} style={style} />} </Tab.Screen> <Tab.Screen name="MapScreen" options={{ title: 'Map', headerShown: false, tabBarIcon: ({ focused }) => ( <Ionicons name={focused ? 'map' : 'map-outline'} size={24} color="black" /> ) }} > {props => <MapScreen {...props} style={style} />} </Tab.Screen> </Tab.Navigator> ); } const Stack = createNativeStackNavigator(); function Navigation({ style }) { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Main" options={{ headerShown: false }} > {props => <Tabs {...props} style={style} />} </Stack.Screen> </Stack.Navigator> </NavigationContainer> ) } export default Navigation;Explanation:
-
Properly Handling Components for Screens: Instead of passing
component={FoodScreen(style)}, we use thechildrenprop ofTab.Screenand pass a function that takespropsand returns the screen component with thestyleprop. -
Passing Props Correctly: We use the function form of the
childrenprop in<Tab.Screen>to ensure the props are passed correctly. -
Ensuring Compatibility: By making these changes, we ensure that the
styleprop is available to the respective screens and the components are correctly rendered.
This setup should fix the issues you're encountering and provide you with a working navigation structure with styles applied appropriately.
-