0

I'm trying to create a JS react function that will return true if a button is clicked and false if it isn't. Essentially like a toggle true or false. I tried this

  export const isButtonClicked = () => {
  const element = document.getElementsByClassName('button');
  if (element.addEventListener('click')) {
    return true;
  } else {
    return false;
  }
};

But it didn't work. I am attaching the function to this

verticalCompact={isButtonClicked()}

Looking for some help. Thanks

  • You can't do that: there's no way to tell the function to "wait" to return until the user does something, and there's no way for the caller to "wait" until the function returns. And what is supposed to happen if the user clicks the button more than once? Or never? A function that returns whether a button was clicked isn't super useful on it's own, what are you actually trying to do here? – Jared Smith Nov 09 '22 at 00:00
  • I'm trying to change the value of something in another file in my react project when a button is clicked – LearningBeNice Nov 09 '22 at 00:09
  • Pass a callback that sets the value. See https://reactjs.org/docs/lifting-state-up.html – Jared Smith Nov 09 '22 at 00:13

0 Answers0