0

Im currently trying to setup a ternary operator condition in my onClick event in Next.js. But im getting an error saying my condition is returning nothing. Please help...

enter image description here

enter image description here

1
  • 1
    DO NOT post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. For more information please see the Meta FAQ entry Why not upload images of code/errors when asking a question? Please edit your question to include the text. Commented Oct 24, 2022 at 18:38

2 Answers 2

2

The onClick handler expects a function, not a function call. Just add an arrow function that calls the alert.

onClick={... ? () => alert('...') : addToCardList};
Sign up to request clarification or add additional context in comments.

Comments

0

You correctly included the addToCart as a function by not calling it.

The problem is that you did not do the same for alert. Your alert is called while rendering, so you are trying to set the onClick function to the return of the alert call.

Replace alert("aaaaa") with () => alert("aaaaaa") to wrap it in a function

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.