Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native - How to create a disabled style for the TouchableOpacity component?

Tags:

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>
like image 526
Fermín Avatar asked Jul 18 '17 14:07

Fermín


People also ask

How do I make Touchableopacity disabled in React Native?

React Native touchableopacity provide a disabled attribute to disable touchableopacity, you have to just pass true in the disabled attribute like the below example.

Can you style Touchableopacity?

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.

How do I disable a component in React Native?

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.

How do you make a button disabled in React Native?

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.


2 Answers

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}
like image 177
Antoine Grandchamp Avatar answered Sep 21 '22 19:09

Antoine Grandchamp


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.

like image 28
Soroush Chehresa Avatar answered Sep 19 '22 19:09

Soroush Chehresa