How do I apply disabled style for a TouchableOpacity component?
<TouchableOpacity
style={styles.buttonWrapper }
onPress={this.userLogin.bind(this)}
disabled={ !this.state.username || !this.state.password }
>
<Text style={styles.buttonText}>Login</Text>
</TouchableOpacity>
React Native touchableopacity provide a disabled attribute to disable touchableopacity, you have to just pass true in the disabled attribute like the below example.
We can style it however we want, just like a View . We can configure the pressed opacity with the activeOpacity prop. This is typically the most common kind of button in a React Native app.
To disable a View component in React Native, we can set the pointerEvents prop to 'none' . to set the inner view's pointerEvents prop to 'none' so that users can't interact with the content inside.
To simply disable the button, we can use the disabled prop in our button element and set its value to true . It is the simplest way to disable any button in react.
The easiest way is to use the same condition as your disabled prop.
Something like this should work :
style={!this.state.username || !this.state.password ? styles.disabled : styles.buttonWrapper}
The best way to have a disabled style for an element in React-Native
or React
is something like this:
style={
(!this.state.username || !this.state.password)
? {...styles.buttonWrapper, ...styles.buttonDisabled}
: styles.buttonWrapper
}
See in action: codesandbox
With using this example, you don't need to have duplicate styles for the button you just need to define disabled style like backgroundColor
or color
for the disabled button in styles.buttonDisabled
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With