0

Hi, I used if condition like below, but there is nothing rendering on the screen. By the way, there is 'MAVI' in leader array

renderall() {           
  return this.state.leader.map(alb =>  {
    if(alb.Renk == 'MAVI') { 
        <View style={styles.container} key={counter = counter + 1}> 
          <Text style={[styles.textStyle, {marginLeft:'5%'}]}> {alb.Tescil_No}  </Text>

          <Text style={[styles.textStyle, {marginLeft:'6%'}]}> {alb.GumrukAdi}  </Text>

          <Text style={[styles.textStyle, { marginLeft:'5%'}]}> {alb.ACIKLAMA}   </Text>                                                
        </View>
    }
  });
}                                    
3
  • You're missing a return statement in your map Commented Feb 15, 2019 at 8:41
  • Hi andrew again you :) , but here is return below renderall() ? Commented Feb 15, 2019 at 8:42
  • You need the first return to return the array of mapped items, you need a return in the .map otherwise nothing is getting mapped, you will just end up with an array of undefined items. See my answer below Commented Feb 15, 2019 at 8:54

1 Answer 1

2

You're missing a return in your .map. Notice that inside the if statement I am returning the items being mapped.

renderall() {           
  return this.state.leader.map(alb =>  {
    if(alb.Renk == 'MAVI') {
      return (    
        <View style={styles.container} key={counter = counter + 1}> 
          <Text style={[styles.textStyle, {marginLeft:'5%'}]}> {alb.Tescil_No}  </Text>

          <Text style={[styles.textStyle, {marginLeft:'6%'}]}> {alb.GumrukAdi}  </Text>

          <Text style={[styles.textStyle, { marginLeft:'5%'}]}> {alb.ACIKLAMA}   </Text>                                                
          </View>
      );
    }
  });
}

This article explains in more detail how the .map function works https://codeburst.io/learn-understand-javascripts-map-function-ffc059264783

Here is a very small example showing how to use the .map function. Notice the return statement.

let leaders = ['Tom','Jerry','Mike'];

let mappedLeaders = leaders.map((leader, index) => {
  return `${leader} is number ${index}`; // notice that I am returning here
})

console.log(mappedLeaders)

Here is an example of using a .map with an if statement inside it. Notice we will get undefined for the first item in the mappedLeaders as we are not returning anything for the first item because Tom is excluded due to the fact that his name is too short.

let leaders = ['Tom','Jerry','Mike'];

let mappedLeaders = leaders.map((leader, index) => {
  if (leader.length > 3) {
    return `${leader} is number ${index}`; // notice that I am returning here
  }
});

console.log(mappedLeaders)

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

Comments

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.