Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
How to call and pass the value to function from another component

I'm trying to call the remove function that is in my ShoppingCartScreen component in my CartList component. I also want to pass in values from the CartList component class. I kept getting issues when I tried to call the add in my CartList class.

export default class ShoppingCartScreen extends Component {
        remove = (qty) => {
            let q = qty
            if (q > 1) {
                q = q - 1;
                this.setState({ qty: q })
            }
    
    
        }
    
    render() {
      return (
              <View>
                <ScrollView backgroundColor='#bbb'>
                   <CartList
                     remove = {this.remove}  />
                    </ScrollView>           
              </View>
                    )
              }
   



export default class CartList extends Component {
 render() {
      
      return (
          <View>
              <TouchableOpacity onPress={this.props.add(this.state.qty)}>
                  <Icon
                       name="md-close"
                       size={25}
                       color='black'
                       style={{ height: 25, width: 25 }}
                       iconStyle={{ marginRight: 0 }}/>
                </TouchableOpacity>
         </View>
       )
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

First, you pass remove to the CartList and then inside call this.props.add, so you need to change the name. Second, you need to upgrade this.props.remove call to an arrow function:

<TouchableOpacity onPress={() => this.props.remove(this.state.qty)}>

onPress expects that you pass a function to be called. When the component is rendered, the this.props.add(this.state.qty) gets evaluated immediately because it is already a function call. This is why you must use an arrow function is this case.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda