Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Native change style on disabled button

How can i change button opacity to 0.5 if the button is disabled? Should i somehow add buttonDisabled style to the TouchableOpacity? What would be the best way and how to do it?

 type IconButtonProps = {
        name: IconN<44>;
        disabled?: boolean;
    }
    
    
    export function IconButton(props: IconButtonProps) {
      return (
        <View style={styles.container} >
          <TouchableOpacity disabled={!props.disabled}>
            <View>
          <Icon size={44} name={props.name} />
          </View>
          </TouchableOpacity>
        </View>
      )
    }
    
    const styles = StyleSheet.create({
        buttonDisabled: {
            opacity: 0.5,
          },
    });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you can either set the style inline like

<TouchableOpacity style={{opacity:props.disabled ? 0.5 : 1}} />

or in stylesheet like:

<TouchableOpacity style={props.disabled ? styles.disabledButton : styles.button} />

there are other options like passing an array of style, etc

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading