Sajal Koley
Sajal Koley

Reputation: 17

How call two function under onPress in react

Here the addToCart function. i want to call this funtion and navigate to another page.

addToCart = () => {
    const {product, quantity, variation, meta_data} = this.state;
    const {dispatch} = this.props;
    let check = true;

    // Check select variations
    if (product.get('type') === productType.VARIABLE) {
      const attributeProduct = product
        .get('attributes')
        .filter(attr => attr.get('variation'));
      if (!meta_data.size || attributeProduct.size !== meta_data.size) {
        check = false;
        showMessage({
          message: 'Please select variations',
          type: 'danger',
        });
      }
    }
    if (check) {
      dispatch(
        addToCart(
          {
            product_id: product.get('id'),
            quantity,
            variation,
            product,
            meta_data,
          },
          () => this.setState({isAddToCart: true}),
        ),
      );
    }
  };

And Render function

<FooterProduct
          isAddToCart={isAddToCart}
          onPressAddCart={this.addToCart}
          onPressBuyNow={

            this.addToCart // function one
            () => navigation.navigate(homeTabs.cart) // function two 


          }
        />

how can I run both? actually I try to create a buy now button. thank you.

Upvotes: 0

Views: 51

Answers (2)

user13637316
user13637316

Reputation:

Try this one:

onPressBuyNow = () => {
  this.addToCart(); // function one
  navigation.navigate(homeTabs.cart); // function two
}

The answer would be either this or create a wrapper function like other folks suggest.


Edit

Modified the answer after Michael Mishin's comment.

Upvotes: 0

Nafiz Ahmed
Nafiz Ahmed

Reputation: 567

Create a custom method that calls both the methods like below

const combinedMethod = () => {
  this.addToCart();
  navigation.navigate(homeTabs.cart);
}

And use the above method like

<FooterProduct
   isAddToCart={isAddToCart}
   onPressAddCart={this.addToCart}
   onPressBuyNow={combinedMethod}
/>

Upvotes: 1

Related Questions