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.