Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
React Native Navigation inside Map Function

I am trying to generate a Card from JSON data using Map function in React Native.

I want to be able to navigate to another page by clicking on this card.

This is the solution I am trying:

function display() {
  
  return restaurant.map((item) => {
    return(
      <TouchableHighlight onPress={() => this.props.navigation.navigate('Restaurant')}>
        <View style={styles.card}>
          <View style={styles.cardHeadText}>
            <Text style={styles.title}>
              { item.name }
            </Text>
            <Text>
              { item.type }
            </Text>
          </View>
        </View>
      </TouchableHighlight>
    );
  });
}

class RestaurantCard extends Component {
  render() {
    return (
      
      <View style={styles.container}>
        {display()}
      </View>
      
      
    );
  }
}

But I get following error:

Undefined is not an object (evaluating '_this.props.navigation')

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can pass this as an argument to the map function as described in the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

function display() {
  
  return restaurant.map((item) => {
    return(
      <TouchableHighlight onPress={() => this.props.navigation.navigate('Restaurant')}>
        <View style={styles.card}>
          <View style={styles.cardHeadText}>
            <Text style={styles.title}>
              { item.name }
            </Text>
            <Text>
              { item.type }
            </Text>
          </View>
        </View>
      </TouchableHighlight>
    );
  }, this); // over here
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can try it :) Ive passed navigation as a props in display function, destructing it and re-using as a short curt to acess this.props.navigation..

Extracting the logic of the click in a handler made it easy to read and control, you can add verifies and other things a lot more easier.

function display(props) {
  const { navigation } = props

  const handlerClick = (item) => {
      /*
         all the content of item (name, and type) will be passed in
         props of the other page component
      */
      navigation.navigate("Restaurant", { ...item})
  }

  return restaurant.map((item) => {
    return(
      <TouchableHighlight onPress={() => handlerClick(item)}>
        <View style={styles.card}>
          <View style={styles.cardHeadText}>
            <Text style={styles.title}>
              { item.name }
            </Text>
            <Text>
              { item.type }
            </Text>
          </View>
        </View>
      </TouchableHighlight>
    );
  });
}

class RestaurantCard extends Component {
  render() {
    return (
      <View style={styles.container}>
        {display(this.props.navigation)}
      </View>
    );
  }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda