2

I have one question. This is probably easy task but I am trying to overthink this. I have such situation:

<Button
 className={favorites ? styles.active : styles.outlines}
 onClick={() => addToFavorites({ id })}
 variant='outline'
>

This button is adding a product as favourite. But i want to prevent page from scrolling. The addToFavorites({ id }) is passed from props. Can i do something like this:

 const addFav = e => {
    e.preventDefault();
  };

and then

<Button
  className={favorites ? styles.active : styles.outlines}
  onClick={e => addToFavorites({ id }, addFav(e))}
  variant='outline'
>

Right now it is workning, but not sure if this is valid solution

2
  • yes, it is correct. Commented Dec 30, 2019 at 9:56
  • You can use disabled property. Commented Dec 30, 2019 at 9:59

2 Answers 2

2

One another way to do it.

<Button className={favorites ? styles.active : styles.outlines} onClick={e => addFav(e,{id}))} variant='outline'>

const addFav = (e,id) => {
   e.preventDefault();
   addToFavorites(id);
};
Sign up to request clarification or add additional context in comments.

2 Comments

but this way i'm overwriting id
but how? can you tell me?
1

You don't need to pass addFav(e) as an argument to addToFavorites. Just call it directly after addToFavorites:

<Button
  className={favorites ? styles.active : styles.outlines}
  onClick={e => (addToFavorites({ id }), addFav(e))}
  variant='outline'
>

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.