Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!