1

I have this array that fetched from backend ! this is the array.

resultes
[
[front] [18:52:18]   Object {
[front] [18:52:18]     "cate_id": 1,
[front] [18:52:18]     "name": "Vehicles",
[front] [18:52:18]   },
[front] [18:52:18]   Object {
[front] [18:52:18]     "cate_id": 2,
[front] [18:52:18]     "name": "Home",
[front] [18:52:18]   },
[front] [18:52:18]   Object {
[front] [18:52:18]     "cate_id": 3,
[front] [18:52:18]     "name": "Electronics",
  },]
 {this.state.categories.results.map((item ,key)=>(
      <View key={key}>

       <Text>{item.name}</Text>
      </View> ))

How can add for every item.name a different icon based on the key of the array? i tried to make an object

const CATEGORIES_TYPES = {
      car: {
        style: { width: 50, fontSize: 40, height: 60, color: '#f56217' }
      },
      home: {
        style: { width: 50, fontSize: 40, height: 60, color: '#2c3e50' }
      },
      tv: {

        style: { width: 50, fontSize: 40, height: 60, color: 'black' }
      }
    };

and in render:

 {this.state.categories.results.map((item ,key)=>(
      <View key={key}>
        {CATEGORIES_TYPES[item.name] && (
        <FontAwesome name={CATEGORIES_TYPES[item.name]} style={CATEGORIES_TYPES[item.name].style} />
      )}
       <Text>{item.name}</Text>
      </View> ))

but that don't work Is this a method to add based on the key???

2
  • can you please add what's in this.state.categories.results? btw, the key used here is just the index of each element in the array, like 0.. 1.. 2.. Commented Nov 2, 2019 at 17:07
  • I added the array Commented Nov 2, 2019 at 17:12

2 Answers 2

1

Use conditional rendering or have an object with the names of the icons and the name of category as their key.

const Icons = {
  Home: 'home',
  Electronics: 'microchip',
  Vehicles: 'car',
};

Then in your render do

{this.state.categories.results.map((item, key) => (
  <View key={key}>
    {CATEGORIES_TYPES[item.name] && (
      <FontAwesome
        name={Icons[item.name]}
        style={CATEGORIES_TYPES[item.name].style}
      />
    )}
    <Text>{item.name}</Text>
  </View>
))}

also change your CATEGORIES_TYPES to look like the keys in your categories object.

const CATEGORIES_TYPES = {
  Vehicles: {
    style: { width: 50, fontSize: 40, height: 60, color: '#f56217' },
  },
  Home: {
    style: { width: 50, fontSize: 40, height: 60, color: '#2c3e50' },
  },
  Electronics: {
    style: { width: 50, fontSize: 40, height: 60, color: 'black' },
  },
};

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

Good Idea,Thank you
Thank youuuuu very much
Excuse me ! How can i make that if i press on any of these categories will navigate to different page ??
add OnPress and navigate to your deisred screen.
I know that but there is for each of categories a special screen!!!
0

I think this is what you want to do.

const styles = StyleSheet.create({
  car: {
        width: 50, 
        fontSize: 40,
        height: 60, 
        color: '#f56217' 
  },
  home: {
        width: 50, 
        fontSize: 40, 
        height: 60,
         color: '#2c3e50' 
  },
  tv: {
       width: 50, 
       fontSize: 40, 
       height: 60, 
       color: 'black' 
  }
});
{this.state.categories.results.map((item ,key)=>(
      <View key={key}>
   <FontAwesome name={item.name} style={item.name === "car" ? styles.car : item.name === "home" ? styles.home : styles.tv} />
       <Text>{item.name}</Text>
      </View> ))

8 Comments

But i want also the name of the icon to be different like the style
@amanirose Like my answer, three different icons are made, and each one has a different style.
@amanirose And you try to organize your style in one place like my answer.
Yes,,like your answer ,,but could we use the key because it is unique
What is your key? You mean the date_id?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.