search

Home  >  Q&A  >  body text

Permanently disable button

<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粉156983446P粉156983446486 days ago634

reply all(1)I'll reply

  • P粉798010441

    P粉7980104412023-07-29 09:55:30

    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.

    reply
    0
  • Cancelreply