Jerry Seigle
Jerry Seigle

Reputation: 457

React JS. remove function from button after button is pressed

How to unmount. Return does not work. I need to be able to press button and after function completes then the function will be remove from the button


  const handleSubmit = () => {
    ipcRenderer.send(
      "EDITOR",
      [ type, typeValue],
      "editor_response"
    );
    onClose(selectedValue);
  };

  return <div>
    <Button onClick={handleSubmit} color="primary">
      Ok
    </Button>
  </div>

Upvotes: 0

Views: 241

Answers (1)

Christian Fritz
Christian Fritz

Reputation: 21364

Probably the cleanest solution is a state variable that determines whether the handler should be active or not.

const MyComp = () => {

  const [pressed, setPressed] = useState(false;

  const handleSubmit = () => {
    setPressed(true);
    ipcRenderer.send(
      "EDITOR",
      [ type, typeValue],
      "editor_response"
    );
    onClose(selectedValue);
  };

  const noop = () => {};
  
  return <div>
    <Button onClick={!pressed ? handleSubmit : noop} color="primary">
      Ok
    </Button>
  </div>;
};

Upvotes: 1

Related Questions