Permanently disable button
P粉156983446
P粉156983446 2023-07-28 21:35:42
0
1
624
<p>In React, can I disable a button when the logged in user is a member? </p><p>When I use the disabled attribute, it can be removed from the developer tools and the button is reactivated. </p><p>Please help me, I want the button to remain disabled when the logged in user is a member. Even if the disabled attribute is removed from the developer tools, the button should still remain disabled. </p><p><em></em><em></em></p>
P粉156983446
P粉156983446

reply all(1)
P粉798010441

You can use the disabled attribute to disable a button, but this does not prevent the user from removing the disabled attribute and re-enabling the button in the development tools. To avoid this, you should also use the disabled value to conditionally handle the button's click event listener.

For example:


import { useState } from "react";

export default function App() {
  const [disabled, setDisabled] = useState(false);
  const handleClick = (e) => {
    console.log(e.target);
  };

  return (
    <div className="App">
      <button onClick={() => setDisabled((disabled) => !disabled)}>
        disable button
      </button>
      <button disabled={disabled} onClick={disabled ? null : handleClick}>
        click
      </button>
    </div>
  );
}

This way, even if the user removes the disabled attribute, the button will not have any click handler attached to it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template