0

Why does this work for the onClick

const mapDispatchToProps = (dispatch) => ({
  toggleCartHidden: () => dispatch(toggleCartHidden()),
});

<Button
  onClick={() => {
    history.push('/checkout');
    toggleCartHidden();
  }}
>Button text</Button>

But this doesnt work (the routing doesnt work)

<Button
  onClick={
    (() => {
      history.push('/checkout');
    },
    toggleCartHidden)
  }
>Button text</Button>

I have another component with similar code that works with this syntax with onClick (no function call within onClick)

const mapDispatchToProps = (dispatch) => ({
  toggleCartHidden: () => dispatch(toggleCartHidden()),
});

<div className='cart-icon' onClick={toggleCartHidden}> ICON </div>

1 Answer 1

1

because you are passing an expression that is evaluated. At expression you use , operator that evaluates each part, but it only returns the last one which is toggleCartHidden.

if you do (1, 3, 5) on your console it returns 5.

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.