I am currently working on implementing the Tab feature of React Navigation and I have encountered a warning message when I try to define the Tab.Navigator component according to the official documentation. In order to customize the tabBarIcon, I followed the code example provided in the documentation:
const Tab = createBottomTabNavigator();
const BottomTabScreen = () => {
return (
<Tab.Navigator
screenOptions={({route}) => ({
tabBarHideOnKeyboard: true,
tabBarShowLabel: false,
headerShown: false,
tabBarStyle: {
height: 70,
},
tabBarIcon: ({focused, size, color}) => {
let iconName;
if (route.name === 'Home') {
iconName = focused ? 'home-sharp' : 'home-outline';
} else if (route.name === 'Search') {
iconName = focused ? 'search' : 'ios-search-outline';
} else if (route.name === 'Activity') {
iconName = focused ? 'ios-heart' : 'ios-heart-outline';
} else if (route.name === 'Profile') {
iconName = focused ? 'ios-person-circle' : 'ios-person-outline';
size = focused ? size + 3 : size;
}
return <Ionic name={iconName} size={size} color={color} />;
},
})}>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Search" component={Search} />
<Tab.Screen name="Activity" component={Activity} />
<Tab.Screen name="Profile" component={Profile} />
</Tab.Navigator>
);
};
However, I am receiving the following warning message:
"Do not define components during render. React will see a new component type on every render and destroy the entire subtree’s DOM nodes and state."
I would like to know if there is a way to resolve this warning message. I would greatly appreciate any help or guidance on this matter. Thank you.
Move the component definition outside of the navigator:
// moved
const TabIcon = ({focused, size, color}) => {
let iconName;
if (route.name === 'Home') {
iconName = focused ? 'home-sharp' : 'home-outline';
} else if (route.name === 'Search') {
iconName = focused ? 'search' : 'ios-search-outline';
} else if (route.name === 'Activity') {
iconName = focused ? 'ios-heart' : 'ios-heart-outline';
} else if (route.name === 'Profile') {
iconName = focused ? 'ios-person-circle' : 'ios-person-outline';
size = focused ? size + 3 : size;
}
return <Ionic name={iconName} size={size} color={color} />;
};
const Tab = createBottomTabNavigator();
const BottomTabScreen = () => {
return (
<Tab.Navigator
screenOptions={({route}) => ({
tabBarHideOnKeyboard: true,
tabBarShowLabel: false,
headerShown: false,
tabBarStyle: {
height: 70,
},
tabBarIcon: (props) => <TabIcon {...props} />, // new
})}>
<Tab.Screen name="Home" component={Home} />
<Tab.Screen name="Search" component={Search} />
<Tab.Screen name="Activity" component={Activity} />
<Tab.Screen name="Profile" component={Profile} />
</Tab.Navigator>
);
};