Can I disable Button in react when login user is member.I waant to disable without using disabled attribute.When I used disabled attribute,it can delete from developer tools and button is active again.Please help me
I want button is disable when login user is member and even delete disabled attribute from developer tool, It still inactive
3
Answers
You can use the
disabled
prop to disable the button, but this will not prevent the user from removing thedisabled
attribute in the dev tools and enabling the button again. To avoid this, you should also use the disabled value to conditionally handle the click event listener from the button. For example:For example:
This way, even if the user removes the
disabled
attribute, the button will not have any click handler attached to it.If you want to disable a button in React based on whether the login user is a member and prevent users from enabling it via developer tools, you need to handle the button’s disabled state in a more secure way. One way to achieve this is by managing the button’s disabled state within your component’s state or using Redux.
Here’s an example of how you can implement this:
Assuming you have some state that indicates whether the login user is
a member or not. For demonstration purposes, I’ll use a boolean
variable called
isMember
to represent this state.Initialize the button’s disabled state based on the
isMember
value.Use React’s lifecycle methods or React hooks (e.g.,
useEffect
) tomonitor changes to the
isMember
value and update the button’sdisabled state accordingly.
When the button is clicked, perform the desired action only if it’s
not disabled.
Disabled button Forever
you can disabled the element and remove the click event listener for the button and like this when they enabling it via developer tools the button dont work as when it is enabled with the orginal function and add an observer to make the button Disabled and this time for Forever
you can try the code at https://codedamn.com/online-compiler/reactjs by copy paste