Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to totally disable a react component?

I have a react component which has some buttons and text inputs. I want to totally disable all the things inside this component until some other works are complete. How to make the entire inner component disabled?

like image 843
hearty Avatar asked Jul 10 '18 07:07

hearty


People also ask

How do you disable an entire component in React?

Button component can be enabled/disabled by giving disabled property. To disable Button component, the disabled property can be set as true .

Can we disable a div in React?

The above code makes the contents of the div disabled, You can make the div disabled by adding the disabled attribute. Upon clicking the button you can change the state variable disabled as 'true'.

How do you disable a card in React?

Better to use form and fieldset , and put all the input/button elements inside that. You can disable all of them by setting the property disabled to fieldset . Refer MDN Doc for more details.

How do you unmount a component?

All you have to do is remove it from the DOM in order to unmount it. As long as renderMyComponent = true , the component will render. If you set renderMyComponent = false , it will unmount from the DOM.


1 Answers

You can add disabled props and use CSS to disable div

const MyComponent = ({disabled}) => {
    return (
        <div style={disabled ? {pointerEvents: "none", opacity: "0.4"} : {}}>
            <h1> Text</h1>
            <input type="text"/>
            <input type="password"/>
            <button>Login</button>
        </div>
    )
}
like image 114
Karo Hovhannisyan Avatar answered Oct 13 '22 11:10

Karo Hovhannisyan