icons from communitymaterialicons not loading

119 views Asked by At

Yesterday I started with react-native and i have problem with icons from react-native-vector-icons/MaterialCommunityIcons, I installed the necessary packages but all the icons are question marks, even changing color or size works

thats my code:

import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Settings from './Settings';
import Exercises from './Exercises';
import Home from './Home';
import Stats from './Stats';
import Workout from './Workout';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'

const Tab = createBottomTabNavigator();

function Tabs (){
    return(
        <Tab.Navigator
        screenOptions={({ route }) => ({
            tabBarIcon: () => {
              const icons: Record<string, string> = {
                Home: "home",
                Stats: "chart-bar",
                Workout: "dumbbell",
                Exercises: "weight-lifter",
                Settings: "cog",
              };
        
              return (
                <MaterialCommunityIcons
                  name={icons[route.name]}
                  color= "black"
                  size={24}
                />
              );
            },
          })}
        >
            <Tab.Screen name="Home" component={Home} />
            <Tab.Screen name="Stats" component={Stats} />
            <Tab.Screen name="Workout" component={Workout} />
            <Tab.Screen name="Exercises" component={Exercises} />
            <Tab.Screen name="Settings" component={Settings} />
        </Tab.Navigator>
    )
}
export default Tabs;

and ss from ios simulator:enter image description here

edit: this article helped me: https://medium.com/clarusway/setting-up-react-native-vector-icons-for-ios-a5d57e78cdb2

1

There are 1 answers

1
talhamaqsood890 On

The problem is this part of code name={icons[route.name]}

if you want to use list of icons, pass index instead of route.name

Code update:

<Tab.Navigator
  screenOptions={({ route, index }) => ({
     tabBarIcon: () => {
      const icons: Record<string, string> = {
           Home: "home",
           Stats: "chart-bar",
           Workout: "dumbbell",
           Exercises: "weight-lifter",
           Settings: "cog",
       };
      
  return (
     <MaterialCommunityIcons
       name={icons[index]}
       color= "black"
       size={24}
     />
   );
  },
  })}
>