DEV Community

Gurupal Singh
Gurupal Singh

Posted on

`onClick` listener to be a function, instead got a value of `object` type.

Here is i am checking on my cart page if user is null than open login popup otherwise go directly to Checkout page.

 var path;
  if (user !== null) {
    path = "/checkout";
  } else {
    path = "";
  }
Enter fullscreen mode Exit fullscreen mode

Here is the button on cart page.

 <button
   type="button"
   onClick={path === "" ? openModal : <></>}>
   <Link href={path}>Checkout</Link>
 </button>
 <Login showModal={showModal} setShowModal={setShowModal} />
Enter fullscreen mode Exit fullscreen mode

But i am getting warnings on console that onClick listener to be a function, instead got a value of object type. Modal is opening but how can i fix this warning ?

Top comments (2)

Collapse
 
arberbr profile image
Arber Braja • Edited

First of all I dont think dev.to is the right place for solving your programming issues.

There are places much more suitable for this.

Secondly you are doing it wrong.

You should provide the onClick event listener a correct event handler, a function. Then encapsulate inside this event handler function whatever logic you want to do. Either return a value from this function or if directly inside the function re-assign url using window.location.assign to whatever value you need to set there.

Collapse
 
zorge1988 profile image
Dima Bondarenko

Error says by itself. You should provide on click a function instead of object. In your cas get rid of button and style link as button or delete link and move logic from link to the onclick